
Use [ATAChietkhau]
Go
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the KhachHangLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_Get_List

AS


				
				SELECT
					[ID],
					[KhachHangID],
					[LoaiMay],
					[DoanhSoToiThieu],
					[IsActive],
					[ThoiDiemLapMay],
					[GhiChu],
					[UpdateDate],
					[UpdatedBy],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangLapMayPhaMau]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the KhachHangLapMayPhaMau table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHangLapMayPhaMau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[KhachHangID], O.[LoaiMay], O.[DoanhSoToiThieu], O.[IsActive], O.[ThoiDiemLapMay], O.[GhiChu], O.[UpdateDate], O.[UpdatedBy], O.[SoBienBan], O.[NgayKyBienBan]
				FROM
				    [dbo].[KhachHangLapMayPhaMau] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHangLapMayPhaMau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the KhachHangLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_Insert
(

	@Id uniqueidentifier   ,

	@KhachHangId uniqueidentifier   ,

	@LoaiMay int   ,

	@DoanhSoToiThieu decimal (18, 0)  ,

	@IsActive bit   ,

	@ThoiDiemLapMay datetime   ,

	@GhiChu nvarchar (MAX)  ,

	@UpdateDate datetime   ,

	@UpdatedBy int   ,

	@SoBienBan nvarchar (50)  ,

	@NgayKyBienBan datetime   
)
AS


					
				INSERT INTO [dbo].[KhachHangLapMayPhaMau]
					(
					[ID]
					,[KhachHangID]
					,[LoaiMay]
					,[DoanhSoToiThieu]
					,[IsActive]
					,[ThoiDiemLapMay]
					,[GhiChu]
					,[UpdateDate]
					,[UpdatedBy]
					,[SoBienBan]
					,[NgayKyBienBan]
					)
				VALUES
					(
					@Id
					,@KhachHangId
					,@LoaiMay
					,@DoanhSoToiThieu
					,@IsActive
					,@ThoiDiemLapMay
					,@GhiChu
					,@UpdateDate
					,@UpdatedBy
					,@SoBienBan
					,@NgayKyBienBan
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the KhachHangLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_Update
(

	@Id uniqueidentifier   ,

	@OriginalId uniqueidentifier   ,

	@KhachHangId uniqueidentifier   ,

	@LoaiMay int   ,

	@DoanhSoToiThieu decimal (18, 0)  ,

	@IsActive bit   ,

	@ThoiDiemLapMay datetime   ,

	@GhiChu nvarchar (MAX)  ,

	@UpdateDate datetime   ,

	@UpdatedBy int   ,

	@SoBienBan nvarchar (50)  ,

	@NgayKyBienBan datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[KhachHangLapMayPhaMau]
				SET
					[ID] = @Id
					,[KhachHangID] = @KhachHangId
					,[LoaiMay] = @LoaiMay
					,[DoanhSoToiThieu] = @DoanhSoToiThieu
					,[IsActive] = @IsActive
					,[ThoiDiemLapMay] = @ThoiDiemLapMay
					,[GhiChu] = @GhiChu
					,[UpdateDate] = @UpdateDate
					,[UpdatedBy] = @UpdatedBy
					,[SoBienBan] = @SoBienBan
					,[NgayKyBienBan] = @NgayKyBienBan
				WHERE
[ID] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the KhachHangLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_Delete
(

	@Id uniqueidentifier   
)
AS


				DELETE FROM [dbo].[KhachHangLapMayPhaMau] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHangLapMayPhaMau table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_GetById
(

	@Id uniqueidentifier   
)
AS


				SELECT
					[ID],
					[KhachHangID],
					[LoaiMay],
					[DoanhSoToiThieu],
					[IsActive],
					[ThoiDiemLapMay],
					[GhiChu],
					[UpdateDate],
					[UpdatedBy],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangLapMayPhaMau]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangLapMayPhaMau_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangLapMayPhaMau_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangLapMayPhaMau_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the KhachHangLapMayPhaMau table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangLapMayPhaMau_Find
(

	@SearchUsingOR bit   = null ,

	@Id uniqueidentifier   = null ,

	@KhachHangId uniqueidentifier   = null ,

	@LoaiMay int   = null ,

	@DoanhSoToiThieu decimal (18, 0)  = null ,

	@IsActive bit   = null ,

	@ThoiDiemLapMay datetime   = null ,

	@GhiChu nvarchar (MAX)  = null ,

	@UpdateDate datetime   = null ,

	@UpdatedBy int   = null ,

	@SoBienBan nvarchar (50)  = null ,

	@NgayKyBienBan datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [KhachHangID]
	, [LoaiMay]
	, [DoanhSoToiThieu]
	, [IsActive]
	, [ThoiDiemLapMay]
	, [GhiChu]
	, [UpdateDate]
	, [UpdatedBy]
	, [SoBienBan]
	, [NgayKyBienBan]
    FROM
	[dbo].[KhachHangLapMayPhaMau]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([KhachHangID] = @KhachHangId OR @KhachHangId is null)
	AND ([LoaiMay] = @LoaiMay OR @LoaiMay is null)
	AND ([DoanhSoToiThieu] = @DoanhSoToiThieu OR @DoanhSoToiThieu is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([ThoiDiemLapMay] = @ThoiDiemLapMay OR @ThoiDiemLapMay is null)
	AND ([GhiChu] = @GhiChu OR @GhiChu is null)
	AND ([UpdateDate] = @UpdateDate OR @UpdateDate is null)
	AND ([UpdatedBy] = @UpdatedBy OR @UpdatedBy is null)
	AND ([SoBienBan] = @SoBienBan OR @SoBienBan is null)
	AND ([NgayKyBienBan] = @NgayKyBienBan OR @NgayKyBienBan is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [KhachHangID]
	, [LoaiMay]
	, [DoanhSoToiThieu]
	, [IsActive]
	, [ThoiDiemLapMay]
	, [GhiChu]
	, [UpdateDate]
	, [UpdatedBy]
	, [SoBienBan]
	, [NgayKyBienBan]
    FROM
	[dbo].[KhachHangLapMayPhaMau]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([KhachHangID] = @KhachHangId AND @KhachHangId is not null)
	OR ([LoaiMay] = @LoaiMay AND @LoaiMay is not null)
	OR ([DoanhSoToiThieu] = @DoanhSoToiThieu AND @DoanhSoToiThieu is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([ThoiDiemLapMay] = @ThoiDiemLapMay AND @ThoiDiemLapMay is not null)
	OR ([GhiChu] = @GhiChu AND @GhiChu is not null)
	OR ([UpdateDate] = @UpdateDate AND @UpdateDate is not null)
	OR ([UpdatedBy] = @UpdatedBy AND @UpdatedBy is not null)
	OR ([SoBienBan] = @SoBienBan AND @SoBienBan is not null)
	OR ([NgayKyBienBan] = @NgayKyBienBan AND @NgayKyBienBan is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChietKhau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_Get_List

AS


				
				SELECT
					[ChietKhauID],
					[StartDate],
					[EndDate],
					[DieuKienGiamGiaID],
					[GhiChu],
					[LastUpdate],
					[UpdateBy],
					[Header],
					[NhomKhachHangID]
				FROM
					[dbo].[ChietKhau]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChietKhau table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChietKhauID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChietKhauID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChietKhauID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChietKhauID], O.[StartDate], O.[EndDate], O.[DieuKienGiamGiaID], O.[GhiChu], O.[LastUpdate], O.[UpdateBy], O.[Header], O.[NhomKhachHangID]
				FROM
				    [dbo].[ChietKhau] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChietKhauID] = PageIndex.[ChietKhauID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChietKhau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_Insert
(

	@ChietKhauId uniqueidentifier   ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@DieuKienGiamGiaId uniqueidentifier   ,

	@GhiChu nvarchar (MAX)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@Header nvarchar (MAX)  ,

	@NhomKhachHangId uniqueidentifier   
)
AS


					
				INSERT INTO [dbo].[ChietKhau]
					(
					[ChietKhauID]
					,[StartDate]
					,[EndDate]
					,[DieuKienGiamGiaID]
					,[GhiChu]
					,[LastUpdate]
					,[UpdateBy]
					,[Header]
					,[NhomKhachHangID]
					)
				VALUES
					(
					@ChietKhauId
					,@StartDate
					,@EndDate
					,@DieuKienGiamGiaId
					,@GhiChu
					,@LastUpdate
					,@UpdateBy
					,@Header
					,@NhomKhachHangId
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChietKhau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_Update
(

	@ChietKhauId uniqueidentifier   ,

	@OriginalChietKhauId uniqueidentifier   ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@DieuKienGiamGiaId uniqueidentifier   ,

	@GhiChu nvarchar (MAX)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@Header nvarchar (MAX)  ,

	@NhomKhachHangId uniqueidentifier   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChietKhau]
				SET
					[ChietKhauID] = @ChietKhauId
					,[StartDate] = @StartDate
					,[EndDate] = @EndDate
					,[DieuKienGiamGiaID] = @DieuKienGiamGiaId
					,[GhiChu] = @GhiChu
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[Header] = @Header
					,[NhomKhachHangID] = @NhomKhachHangId
				WHERE
[ChietKhauID] = @OriginalChietKhauId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChietKhau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_Delete
(

	@ChietKhauId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[ChietKhau] WITH (ROWLOCK) 
				WHERE
					[ChietKhauID] = @ChietKhauId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_GetByNhomKhachHangId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_GetByNhomKhachHangId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_GetByNhomKhachHangId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhau table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_GetByNhomKhachHangId
(

	@NhomKhachHangId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauID],
					[StartDate],
					[EndDate],
					[DieuKienGiamGiaID],
					[GhiChu],
					[LastUpdate],
					[UpdateBy],
					[Header],
					[NhomKhachHangID]
				FROM
					[dbo].[ChietKhau]
				WHERE
					[NhomKhachHangID] = @NhomKhachHangId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_GetByDieuKienGiamGiaId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_GetByDieuKienGiamGiaId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_GetByDieuKienGiamGiaId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhau table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_GetByDieuKienGiamGiaId
(

	@DieuKienGiamGiaId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauID],
					[StartDate],
					[EndDate],
					[DieuKienGiamGiaID],
					[GhiChu],
					[LastUpdate],
					[UpdateBy],
					[Header],
					[NhomKhachHangID]
				FROM
					[dbo].[ChietKhau]
				WHERE
					[DieuKienGiamGiaID] = @DieuKienGiamGiaId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_GetByChietKhauId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_GetByChietKhauId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_GetByChietKhauId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhau table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_GetByChietKhauId
(

	@ChietKhauId uniqueidentifier   
)
AS


				SELECT
					[ChietKhauID],
					[StartDate],
					[EndDate],
					[DieuKienGiamGiaID],
					[GhiChu],
					[LastUpdate],
					[UpdateBy],
					[Header],
					[NhomKhachHangID]
				FROM
					[dbo].[ChietKhau]
				WHERE
					[ChietKhauID] = @ChietKhauId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhau_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhau_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhau_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChietKhau table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhau_Find
(

	@SearchUsingOR bit   = null ,

	@ChietKhauId uniqueidentifier   = null ,

	@StartDate datetime   = null ,

	@EndDate datetime   = null ,

	@DieuKienGiamGiaId uniqueidentifier   = null ,

	@GhiChu nvarchar (MAX)  = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@Header nvarchar (MAX)  = null ,

	@NhomKhachHangId uniqueidentifier   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChietKhauID]
	, [StartDate]
	, [EndDate]
	, [DieuKienGiamGiaID]
	, [GhiChu]
	, [LastUpdate]
	, [UpdateBy]
	, [Header]
	, [NhomKhachHangID]
    FROM
	[dbo].[ChietKhau]
    WHERE 
	 ([ChietKhauID] = @ChietKhauId OR @ChietKhauId is null)
	AND ([StartDate] = @StartDate OR @StartDate is null)
	AND ([EndDate] = @EndDate OR @EndDate is null)
	AND ([DieuKienGiamGiaID] = @DieuKienGiamGiaId OR @DieuKienGiamGiaId is null)
	AND ([GhiChu] = @GhiChu OR @GhiChu is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([Header] = @Header OR @Header is null)
	AND ([NhomKhachHangID] = @NhomKhachHangId OR @NhomKhachHangId is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChietKhauID]
	, [StartDate]
	, [EndDate]
	, [DieuKienGiamGiaID]
	, [GhiChu]
	, [LastUpdate]
	, [UpdateBy]
	, [Header]
	, [NhomKhachHangID]
    FROM
	[dbo].[ChietKhau]
    WHERE 
	 ([ChietKhauID] = @ChietKhauId AND @ChietKhauId is not null)
	OR ([StartDate] = @StartDate AND @StartDate is not null)
	OR ([EndDate] = @EndDate AND @EndDate is not null)
	OR ([DieuKienGiamGiaID] = @DieuKienGiamGiaId AND @DieuKienGiamGiaId is not null)
	OR ([GhiChu] = @GhiChu AND @GhiChu is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([Header] = @Header AND @Header is not null)
	OR ([NhomKhachHangID] = @NhomKhachHangId AND @NhomKhachHangId is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the KhachHangDuthuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_Get_List

AS


				
				SELECT
					[ID],
					[HoiVienID],
					[Duration],
					[StartDate],
					[EndDate],
					[BrandID],
					[IsActive],
					[ChuongTrinhID],
					[LoaiSanPham],
					[MucChietKhauTD],
					[MucChietKhauCC],
					[UpdatedDate],
					[UpdatedBy],
					[PhanThuongID],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangDuthuong]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the KhachHangDuthuong table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHangDuthuong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[HoiVienID], O.[Duration], O.[StartDate], O.[EndDate], O.[BrandID], O.[IsActive], O.[ChuongTrinhID], O.[LoaiSanPham], O.[MucChietKhauTD], O.[MucChietKhauCC], O.[UpdatedDate], O.[UpdatedBy], O.[PhanThuongID], O.[SoBienBan], O.[NgayKyBienBan]
				FROM
				    [dbo].[KhachHangDuthuong] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHangDuthuong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the KhachHangDuthuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_Insert
(

	@Id uniqueidentifier   ,

	@HoiVienId uniqueidentifier   ,

	@Duration int   ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@BrandId int   ,

	@IsActive bit   ,

	@ChuongTrinhId int   ,

	@LoaiSanPham nchar (10)  ,

	@MucChietKhauTd decimal (18, 0)  ,

	@MucChietKhauCc decimal (18, 0)  ,

	@UpdatedDate datetime   ,

	@UpdatedBy int   ,

	@PhanThuongId int   ,

	@SoBienBan nvarchar (50)  ,

	@NgayKyBienBan datetime   
)
AS


					
				INSERT INTO [dbo].[KhachHangDuthuong]
					(
					[ID]
					,[HoiVienID]
					,[Duration]
					,[StartDate]
					,[EndDate]
					,[BrandID]
					,[IsActive]
					,[ChuongTrinhID]
					,[LoaiSanPham]
					,[MucChietKhauTD]
					,[MucChietKhauCC]
					,[UpdatedDate]
					,[UpdatedBy]
					,[PhanThuongID]
					,[SoBienBan]
					,[NgayKyBienBan]
					)
				VALUES
					(
					@Id
					,@HoiVienId
					,@Duration
					,@StartDate
					,@EndDate
					,@BrandId
					,@IsActive
					,@ChuongTrinhId
					,@LoaiSanPham
					,@MucChietKhauTd
					,@MucChietKhauCc
					,@UpdatedDate
					,@UpdatedBy
					,@PhanThuongId
					,@SoBienBan
					,@NgayKyBienBan
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the KhachHangDuthuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_Update
(

	@Id uniqueidentifier   ,

	@OriginalId uniqueidentifier   ,

	@HoiVienId uniqueidentifier   ,

	@Duration int   ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@BrandId int   ,

	@IsActive bit   ,

	@ChuongTrinhId int   ,

	@LoaiSanPham nchar (10)  ,

	@MucChietKhauTd decimal (18, 0)  ,

	@MucChietKhauCc decimal (18, 0)  ,

	@UpdatedDate datetime   ,

	@UpdatedBy int   ,

	@PhanThuongId int   ,

	@SoBienBan nvarchar (50)  ,

	@NgayKyBienBan datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[KhachHangDuthuong]
				SET
					[ID] = @Id
					,[HoiVienID] = @HoiVienId
					,[Duration] = @Duration
					,[StartDate] = @StartDate
					,[EndDate] = @EndDate
					,[BrandID] = @BrandId
					,[IsActive] = @IsActive
					,[ChuongTrinhID] = @ChuongTrinhId
					,[LoaiSanPham] = @LoaiSanPham
					,[MucChietKhauTD] = @MucChietKhauTd
					,[MucChietKhauCC] = @MucChietKhauCc
					,[UpdatedDate] = @UpdatedDate
					,[UpdatedBy] = @UpdatedBy
					,[PhanThuongID] = @PhanThuongId
					,[SoBienBan] = @SoBienBan
					,[NgayKyBienBan] = @NgayKyBienBan
				WHERE
[ID] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the KhachHangDuthuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_Delete
(

	@Id uniqueidentifier   
)
AS


				DELETE FROM [dbo].[KhachHangDuthuong] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_GetByChuongTrinhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_GetByChuongTrinhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_GetByChuongTrinhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHangDuthuong table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_GetByChuongTrinhId
(

	@ChuongTrinhId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ID],
					[HoiVienID],
					[Duration],
					[StartDate],
					[EndDate],
					[BrandID],
					[IsActive],
					[ChuongTrinhID],
					[LoaiSanPham],
					[MucChietKhauTD],
					[MucChietKhauCC],
					[UpdatedDate],
					[UpdatedBy],
					[PhanThuongID],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangDuthuong]
				WHERE
					[ChuongTrinhID] = @ChuongTrinhId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_GetByBrandId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_GetByBrandId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_GetByBrandId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHangDuthuong table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_GetByBrandId
(

	@BrandId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ID],
					[HoiVienID],
					[Duration],
					[StartDate],
					[EndDate],
					[BrandID],
					[IsActive],
					[ChuongTrinhID],
					[LoaiSanPham],
					[MucChietKhauTD],
					[MucChietKhauCC],
					[UpdatedDate],
					[UpdatedBy],
					[PhanThuongID],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangDuthuong]
				WHERE
					[BrandID] = @BrandId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_GetByPhanThuongId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_GetByPhanThuongId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_GetByPhanThuongId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHangDuthuong table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_GetByPhanThuongId
(

	@PhanThuongId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ID],
					[HoiVienID],
					[Duration],
					[StartDate],
					[EndDate],
					[BrandID],
					[IsActive],
					[ChuongTrinhID],
					[LoaiSanPham],
					[MucChietKhauTD],
					[MucChietKhauCC],
					[UpdatedDate],
					[UpdatedBy],
					[PhanThuongID],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangDuthuong]
				WHERE
					[PhanThuongID] = @PhanThuongId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHangDuthuong table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_GetById
(

	@Id uniqueidentifier   
)
AS


				SELECT
					[ID],
					[HoiVienID],
					[Duration],
					[StartDate],
					[EndDate],
					[BrandID],
					[IsActive],
					[ChuongTrinhID],
					[LoaiSanPham],
					[MucChietKhauTD],
					[MucChietKhauCC],
					[UpdatedDate],
					[UpdatedBy],
					[PhanThuongID],
					[SoBienBan],
					[NgayKyBienBan]
				FROM
					[dbo].[KhachHangDuthuong]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHangDuthuong_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHangDuthuong_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHangDuthuong_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the KhachHangDuthuong table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHangDuthuong_Find
(

	@SearchUsingOR bit   = null ,

	@Id uniqueidentifier   = null ,

	@HoiVienId uniqueidentifier   = null ,

	@Duration int   = null ,

	@StartDate datetime   = null ,

	@EndDate datetime   = null ,

	@BrandId int   = null ,

	@IsActive bit   = null ,

	@ChuongTrinhId int   = null ,

	@LoaiSanPham nchar (10)  = null ,

	@MucChietKhauTd decimal (18, 0)  = null ,

	@MucChietKhauCc decimal (18, 0)  = null ,

	@UpdatedDate datetime   = null ,

	@UpdatedBy int   = null ,

	@PhanThuongId int   = null ,

	@SoBienBan nvarchar (50)  = null ,

	@NgayKyBienBan datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [HoiVienID]
	, [Duration]
	, [StartDate]
	, [EndDate]
	, [BrandID]
	, [IsActive]
	, [ChuongTrinhID]
	, [LoaiSanPham]
	, [MucChietKhauTD]
	, [MucChietKhauCC]
	, [UpdatedDate]
	, [UpdatedBy]
	, [PhanThuongID]
	, [SoBienBan]
	, [NgayKyBienBan]
    FROM
	[dbo].[KhachHangDuthuong]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([HoiVienID] = @HoiVienId OR @HoiVienId is null)
	AND ([Duration] = @Duration OR @Duration is null)
	AND ([StartDate] = @StartDate OR @StartDate is null)
	AND ([EndDate] = @EndDate OR @EndDate is null)
	AND ([BrandID] = @BrandId OR @BrandId is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([ChuongTrinhID] = @ChuongTrinhId OR @ChuongTrinhId is null)
	AND ([LoaiSanPham] = @LoaiSanPham OR @LoaiSanPham is null)
	AND ([MucChietKhauTD] = @MucChietKhauTd OR @MucChietKhauTd is null)
	AND ([MucChietKhauCC] = @MucChietKhauCc OR @MucChietKhauCc is null)
	AND ([UpdatedDate] = @UpdatedDate OR @UpdatedDate is null)
	AND ([UpdatedBy] = @UpdatedBy OR @UpdatedBy is null)
	AND ([PhanThuongID] = @PhanThuongId OR @PhanThuongId is null)
	AND ([SoBienBan] = @SoBienBan OR @SoBienBan is null)
	AND ([NgayKyBienBan] = @NgayKyBienBan OR @NgayKyBienBan is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [HoiVienID]
	, [Duration]
	, [StartDate]
	, [EndDate]
	, [BrandID]
	, [IsActive]
	, [ChuongTrinhID]
	, [LoaiSanPham]
	, [MucChietKhauTD]
	, [MucChietKhauCC]
	, [UpdatedDate]
	, [UpdatedBy]
	, [PhanThuongID]
	, [SoBienBan]
	, [NgayKyBienBan]
    FROM
	[dbo].[KhachHangDuthuong]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([HoiVienID] = @HoiVienId AND @HoiVienId is not null)
	OR ([Duration] = @Duration AND @Duration is not null)
	OR ([StartDate] = @StartDate AND @StartDate is not null)
	OR ([EndDate] = @EndDate AND @EndDate is not null)
	OR ([BrandID] = @BrandId AND @BrandId is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([ChuongTrinhID] = @ChuongTrinhId AND @ChuongTrinhId is not null)
	OR ([LoaiSanPham] = @LoaiSanPham AND @LoaiSanPham is not null)
	OR ([MucChietKhauTD] = @MucChietKhauTd AND @MucChietKhauTd is not null)
	OR ([MucChietKhauCC] = @MucChietKhauCc AND @MucChietKhauCc is not null)
	OR ([UpdatedDate] = @UpdatedDate AND @UpdatedDate is not null)
	OR ([UpdatedBy] = @UpdatedBy AND @UpdatedBy is not null)
	OR ([PhanThuongID] = @PhanThuongId AND @PhanThuongId is not null)
	OR ([SoBienBan] = @SoBienBan AND @SoBienBan is not null)
	OR ([NgayKyBienBan] = @NgayKyBienBan AND @NgayKyBienBan is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the LoaiKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_Get_List

AS


				
				SELECT
					[LoaiKhachHangID],
					[TenLoaiKhachHang],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[LoaiKhachHang]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the LoaiKhachHang table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [LoaiKhachHangID] tinyint 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([LoaiKhachHangID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [LoaiKhachHangID]'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiKhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[LoaiKhachHangID], O.[TenLoaiKhachHang], O.[IsActive], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[LoaiKhachHang] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[LoaiKhachHangID] = PageIndex.[LoaiKhachHangID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiKhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the LoaiKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_Insert
(

	@LoaiKhachHangId tinyint    OUTPUT,

	@TenLoaiKhachHang nvarchar (100)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[LoaiKhachHang]
					(
					[TenLoaiKhachHang]
					,[IsActive]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@TenLoaiKhachHang
					,@IsActive
					,@LastUpdate
					,@UpdateBy
					)
				
				-- Get the identity value
				SET @LoaiKhachHangId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the LoaiKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_Update
(

	@LoaiKhachHangId tinyint   ,

	@TenLoaiKhachHang nvarchar (100)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[LoaiKhachHang]
				SET
					[TenLoaiKhachHang] = @TenLoaiKhachHang
					,[IsActive] = @IsActive
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[LoaiKhachHangID] = @LoaiKhachHangId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the LoaiKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_Delete
(

	@LoaiKhachHangId tinyint   
)
AS


				DELETE FROM [dbo].[LoaiKhachHang] WITH (ROWLOCK) 
				WHERE
					[LoaiKhachHangID] = @LoaiKhachHangId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_GetByLoaiKhachHangId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_GetByLoaiKhachHangId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_GetByLoaiKhachHangId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the LoaiKhachHang table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_GetByLoaiKhachHangId
(

	@LoaiKhachHangId tinyint   
)
AS


				SELECT
					[LoaiKhachHangID],
					[TenLoaiKhachHang],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[LoaiKhachHang]
				WHERE
					[LoaiKhachHangID] = @LoaiKhachHangId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiKhachHang_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiKhachHang_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiKhachHang_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the LoaiKhachHang table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiKhachHang_Find
(

	@SearchUsingOR bit   = null ,

	@LoaiKhachHangId tinyint   = null ,

	@TenLoaiKhachHang nvarchar (100)  = null ,

	@IsActive bit   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [LoaiKhachHangID]
	, [TenLoaiKhachHang]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[LoaiKhachHang]
    WHERE 
	 ([LoaiKhachHangID] = @LoaiKhachHangId OR @LoaiKhachHangId is null)
	AND ([TenLoaiKhachHang] = @TenLoaiKhachHang OR @TenLoaiKhachHang is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [LoaiKhachHangID]
	, [TenLoaiKhachHang]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[LoaiKhachHang]
    WHERE 
	 ([LoaiKhachHangID] = @LoaiKhachHangId AND @LoaiKhachHangId is not null)
	OR ([TenLoaiKhachHang] = @TenLoaiKhachHang AND @TenLoaiKhachHang is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the KhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_Get_List

AS


				
				SELECT
					[KhachHangID],
					[Ten_KH],
					[Gioi_Tinh],
					[Ngay_Sinh],
					[Ma_KH],
					[CMND],
					[Ngay_Cap],
					[Noi_Cap],
					[So_DT_DK],
					[So_DT_CTY],
					[Fax],
					[So_GPDKKD],
					[Dia_Chi_TT],
					[Ho_Khau_TT],
					[Parent_ID],
					[Dia_Chi_Cty],
					[BranchID],
					[Loai_KH],
					[MaSoHV],
					[NgayDangKy]
				FROM
					[dbo].[KhachHang]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the KhachHang table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [KhachHangID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([KhachHangID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [KhachHangID]'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[KhachHangID], O.[Ten_KH], O.[Gioi_Tinh], O.[Ngay_Sinh], O.[Ma_KH], O.[CMND], O.[Ngay_Cap], O.[Noi_Cap], O.[So_DT_DK], O.[So_DT_CTY], O.[Fax], O.[So_GPDKKD], O.[Dia_Chi_TT], O.[Ho_Khau_TT], O.[Parent_ID], O.[Dia_Chi_Cty], O.[BranchID], O.[Loai_KH], O.[MaSoHV], O.[NgayDangKy]
				FROM
				    [dbo].[KhachHang] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[KhachHangID] = PageIndex.[KhachHangID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[KhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the KhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_Insert
(

	@KhachHangId uniqueidentifier   ,

	@TenKh nvarchar (200)  ,

	@GioiTinh nvarchar (50)  ,

	@NgaySinh datetime   ,

	@MaKh varchar (50)  ,

	@Cmnd varchar (50)  ,

	@NgayCap datetime   ,

	@NoiCap nvarchar (50)  ,

	@SoDtDk varchar (50)  ,

	@SoDtCty varchar (50)  ,

	@Fax varchar (50)  ,

	@SoGpdkkd varchar (50)  ,

	@DiaChiTt nvarchar (200)  ,

	@HoKhauTt nvarchar (200)  ,

	@ParentId uniqueidentifier   ,

	@DiaChiCty nvarchar (200)  ,

	@BranchId uniqueidentifier   ,

	@LoaiKh smallint   ,

	@MaSoHv varchar (50)  ,

	@NgayDangKy datetime   
)
AS


					
				INSERT INTO [dbo].[KhachHang]
					(
					[KhachHangID]
					,[Ten_KH]
					,[Gioi_Tinh]
					,[Ngay_Sinh]
					,[Ma_KH]
					,[CMND]
					,[Ngay_Cap]
					,[Noi_Cap]
					,[So_DT_DK]
					,[So_DT_CTY]
					,[Fax]
					,[So_GPDKKD]
					,[Dia_Chi_TT]
					,[Ho_Khau_TT]
					,[Parent_ID]
					,[Dia_Chi_Cty]
					,[BranchID]
					,[Loai_KH]
					,[MaSoHV]
					,[NgayDangKy]
					)
				VALUES
					(
					@KhachHangId
					,@TenKh
					,@GioiTinh
					,@NgaySinh
					,@MaKh
					,@Cmnd
					,@NgayCap
					,@NoiCap
					,@SoDtDk
					,@SoDtCty
					,@Fax
					,@SoGpdkkd
					,@DiaChiTt
					,@HoKhauTt
					,@ParentId
					,@DiaChiCty
					,@BranchId
					,@LoaiKh
					,@MaSoHv
					,@NgayDangKy
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the KhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_Update
(

	@KhachHangId uniqueidentifier   ,

	@OriginalKhachHangId uniqueidentifier   ,

	@TenKh nvarchar (200)  ,

	@GioiTinh nvarchar (50)  ,

	@NgaySinh datetime   ,

	@MaKh varchar (50)  ,

	@Cmnd varchar (50)  ,

	@NgayCap datetime   ,

	@NoiCap nvarchar (50)  ,

	@SoDtDk varchar (50)  ,

	@SoDtCty varchar (50)  ,

	@Fax varchar (50)  ,

	@SoGpdkkd varchar (50)  ,

	@DiaChiTt nvarchar (200)  ,

	@HoKhauTt nvarchar (200)  ,

	@ParentId uniqueidentifier   ,

	@DiaChiCty nvarchar (200)  ,

	@BranchId uniqueidentifier   ,

	@LoaiKh smallint   ,

	@MaSoHv varchar (50)  ,

	@NgayDangKy datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[KhachHang]
				SET
					[KhachHangID] = @KhachHangId
					,[Ten_KH] = @TenKh
					,[Gioi_Tinh] = @GioiTinh
					,[Ngay_Sinh] = @NgaySinh
					,[Ma_KH] = @MaKh
					,[CMND] = @Cmnd
					,[Ngay_Cap] = @NgayCap
					,[Noi_Cap] = @NoiCap
					,[So_DT_DK] = @SoDtDk
					,[So_DT_CTY] = @SoDtCty
					,[Fax] = @Fax
					,[So_GPDKKD] = @SoGpdkkd
					,[Dia_Chi_TT] = @DiaChiTt
					,[Ho_Khau_TT] = @HoKhauTt
					,[Parent_ID] = @ParentId
					,[Dia_Chi_Cty] = @DiaChiCty
					,[BranchID] = @BranchId
					,[Loai_KH] = @LoaiKh
					,[MaSoHV] = @MaSoHv
					,[NgayDangKy] = @NgayDangKy
				WHERE
[KhachHangID] = @OriginalKhachHangId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the KhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_Delete
(

	@KhachHangId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[KhachHang] WITH (ROWLOCK) 
				WHERE
					[KhachHangID] = @KhachHangId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_GetByKhachHangId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_GetByKhachHangId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_GetByKhachHangId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the KhachHang table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_GetByKhachHangId
(

	@KhachHangId uniqueidentifier   
)
AS


				SELECT
					[KhachHangID],
					[Ten_KH],
					[Gioi_Tinh],
					[Ngay_Sinh],
					[Ma_KH],
					[CMND],
					[Ngay_Cap],
					[Noi_Cap],
					[So_DT_DK],
					[So_DT_CTY],
					[Fax],
					[So_GPDKKD],
					[Dia_Chi_TT],
					[Ho_Khau_TT],
					[Parent_ID],
					[Dia_Chi_Cty],
					[BranchID],
					[Loai_KH],
					[MaSoHV],
					[NgayDangKy]
				FROM
					[dbo].[KhachHang]
				WHERE
					[KhachHangID] = @KhachHangId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.KhachHang_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.KhachHang_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.KhachHang_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the KhachHang table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.KhachHang_Find
(

	@SearchUsingOR bit   = null ,

	@KhachHangId uniqueidentifier   = null ,

	@TenKh nvarchar (200)  = null ,

	@GioiTinh nvarchar (50)  = null ,

	@NgaySinh datetime   = null ,

	@MaKh varchar (50)  = null ,

	@Cmnd varchar (50)  = null ,

	@NgayCap datetime   = null ,

	@NoiCap nvarchar (50)  = null ,

	@SoDtDk varchar (50)  = null ,

	@SoDtCty varchar (50)  = null ,

	@Fax varchar (50)  = null ,

	@SoGpdkkd varchar (50)  = null ,

	@DiaChiTt nvarchar (200)  = null ,

	@HoKhauTt nvarchar (200)  = null ,

	@ParentId uniqueidentifier   = null ,

	@DiaChiCty nvarchar (200)  = null ,

	@BranchId uniqueidentifier   = null ,

	@LoaiKh smallint   = null ,

	@MaSoHv varchar (50)  = null ,

	@NgayDangKy datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [KhachHangID]
	, [Ten_KH]
	, [Gioi_Tinh]
	, [Ngay_Sinh]
	, [Ma_KH]
	, [CMND]
	, [Ngay_Cap]
	, [Noi_Cap]
	, [So_DT_DK]
	, [So_DT_CTY]
	, [Fax]
	, [So_GPDKKD]
	, [Dia_Chi_TT]
	, [Ho_Khau_TT]
	, [Parent_ID]
	, [Dia_Chi_Cty]
	, [BranchID]
	, [Loai_KH]
	, [MaSoHV]
	, [NgayDangKy]
    FROM
	[dbo].[KhachHang]
    WHERE 
	 ([KhachHangID] = @KhachHangId OR @KhachHangId is null)
	AND ([Ten_KH] = @TenKh OR @TenKh is null)
	AND ([Gioi_Tinh] = @GioiTinh OR @GioiTinh is null)
	AND ([Ngay_Sinh] = @NgaySinh OR @NgaySinh is null)
	AND ([Ma_KH] = @MaKh OR @MaKh is null)
	AND ([CMND] = @Cmnd OR @Cmnd is null)
	AND ([Ngay_Cap] = @NgayCap OR @NgayCap is null)
	AND ([Noi_Cap] = @NoiCap OR @NoiCap is null)
	AND ([So_DT_DK] = @SoDtDk OR @SoDtDk is null)
	AND ([So_DT_CTY] = @SoDtCty OR @SoDtCty is null)
	AND ([Fax] = @Fax OR @Fax is null)
	AND ([So_GPDKKD] = @SoGpdkkd OR @SoGpdkkd is null)
	AND ([Dia_Chi_TT] = @DiaChiTt OR @DiaChiTt is null)
	AND ([Ho_Khau_TT] = @HoKhauTt OR @HoKhauTt is null)
	AND ([Parent_ID] = @ParentId OR @ParentId is null)
	AND ([Dia_Chi_Cty] = @DiaChiCty OR @DiaChiCty is null)
	AND ([BranchID] = @BranchId OR @BranchId is null)
	AND ([Loai_KH] = @LoaiKh OR @LoaiKh is null)
	AND ([MaSoHV] = @MaSoHv OR @MaSoHv is null)
	AND ([NgayDangKy] = @NgayDangKy OR @NgayDangKy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [KhachHangID]
	, [Ten_KH]
	, [Gioi_Tinh]
	, [Ngay_Sinh]
	, [Ma_KH]
	, [CMND]
	, [Ngay_Cap]
	, [Noi_Cap]
	, [So_DT_DK]
	, [So_DT_CTY]
	, [Fax]
	, [So_GPDKKD]
	, [Dia_Chi_TT]
	, [Ho_Khau_TT]
	, [Parent_ID]
	, [Dia_Chi_Cty]
	, [BranchID]
	, [Loai_KH]
	, [MaSoHV]
	, [NgayDangKy]
    FROM
	[dbo].[KhachHang]
    WHERE 
	 ([KhachHangID] = @KhachHangId AND @KhachHangId is not null)
	OR ([Ten_KH] = @TenKh AND @TenKh is not null)
	OR ([Gioi_Tinh] = @GioiTinh AND @GioiTinh is not null)
	OR ([Ngay_Sinh] = @NgaySinh AND @NgaySinh is not null)
	OR ([Ma_KH] = @MaKh AND @MaKh is not null)
	OR ([CMND] = @Cmnd AND @Cmnd is not null)
	OR ([Ngay_Cap] = @NgayCap AND @NgayCap is not null)
	OR ([Noi_Cap] = @NoiCap AND @NoiCap is not null)
	OR ([So_DT_DK] = @SoDtDk AND @SoDtDk is not null)
	OR ([So_DT_CTY] = @SoDtCty AND @SoDtCty is not null)
	OR ([Fax] = @Fax AND @Fax is not null)
	OR ([So_GPDKKD] = @SoGpdkkd AND @SoGpdkkd is not null)
	OR ([Dia_Chi_TT] = @DiaChiTt AND @DiaChiTt is not null)
	OR ([Ho_Khau_TT] = @HoKhauTt AND @HoKhauTt is not null)
	OR ([Parent_ID] = @ParentId AND @ParentId is not null)
	OR ([Dia_Chi_Cty] = @DiaChiCty AND @DiaChiCty is not null)
	OR ([BranchID] = @BranchId AND @BranchId is not null)
	OR ([Loai_KH] = @LoaiKh AND @LoaiKh is not null)
	OR ([MaSoHV] = @MaSoHv AND @MaSoHv is not null)
	OR ([NgayDangKy] = @NgayDangKy AND @NgayDangKy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the LoaiThoiGian table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_Get_List

AS


				
				SELECT
					[LoaiThoiGianID],
					[TenLoaiThoiGian],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[LoaiThoiGian]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the LoaiThoiGian table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [LoaiThoiGianID] tinyint 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([LoaiThoiGianID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [LoaiThoiGianID]'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiThoiGian]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[LoaiThoiGianID], O.[TenLoaiThoiGian], O.[IsActive], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[LoaiThoiGian] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[LoaiThoiGianID] = PageIndex.[LoaiThoiGianID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiThoiGian]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the LoaiThoiGian table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_Insert
(

	@LoaiThoiGianId tinyint    OUTPUT,

	@TenLoaiThoiGian nvarchar (50)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[LoaiThoiGian]
					(
					[TenLoaiThoiGian]
					,[IsActive]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@TenLoaiThoiGian
					,@IsActive
					,@LastUpdate
					,@UpdateBy
					)
				
				-- Get the identity value
				SET @LoaiThoiGianId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the LoaiThoiGian table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_Update
(

	@LoaiThoiGianId tinyint   ,

	@TenLoaiThoiGian nvarchar (50)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[LoaiThoiGian]
				SET
					[TenLoaiThoiGian] = @TenLoaiThoiGian
					,[IsActive] = @IsActive
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[LoaiThoiGianID] = @LoaiThoiGianId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the LoaiThoiGian table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_Delete
(

	@LoaiThoiGianId tinyint   
)
AS


				DELETE FROM [dbo].[LoaiThoiGian] WITH (ROWLOCK) 
				WHERE
					[LoaiThoiGianID] = @LoaiThoiGianId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_GetByLoaiThoiGianId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_GetByLoaiThoiGianId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_GetByLoaiThoiGianId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the LoaiThoiGian table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_GetByLoaiThoiGianId
(

	@LoaiThoiGianId tinyint   
)
AS


				SELECT
					[LoaiThoiGianID],
					[TenLoaiThoiGian],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[LoaiThoiGian]
				WHERE
					[LoaiThoiGianID] = @LoaiThoiGianId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiThoiGian_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiThoiGian_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiThoiGian_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the LoaiThoiGian table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiThoiGian_Find
(

	@SearchUsingOR bit   = null ,

	@LoaiThoiGianId tinyint   = null ,

	@TenLoaiThoiGian nvarchar (50)  = null ,

	@IsActive bit   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [LoaiThoiGianID]
	, [TenLoaiThoiGian]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[LoaiThoiGian]
    WHERE 
	 ([LoaiThoiGianID] = @LoaiThoiGianId OR @LoaiThoiGianId is null)
	AND ([TenLoaiThoiGian] = @TenLoaiThoiGian OR @TenLoaiThoiGian is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [LoaiThoiGianID]
	, [TenLoaiThoiGian]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[LoaiThoiGian]
    WHERE 
	 ([LoaiThoiGianID] = @LoaiThoiGianId AND @LoaiThoiGianId is not null)
	OR ([TenLoaiThoiGian] = @TenLoaiThoiGian AND @TenLoaiThoiGian is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the Revenue table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_Get_List

AS


				
				SELECT
					[ID],
					[Order_Num],
					[Order_Date],
					[Customer_ID],
					[Popular],
					[Deluxe],
					[Note],
					[Year],
					[Month],
					[BrandCode]
				FROM
					[dbo].[Revenue]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the Revenue table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Revenue]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[Order_Num], O.[Order_Date], O.[Customer_ID], O.[Popular], O.[Deluxe], O.[Note], O.[Year], O.[Month], O.[BrandCode]
				FROM
				    [dbo].[Revenue] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Revenue]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the Revenue table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_Insert
(

	@Id int    OUTPUT,

	@OrderNum varchar (50)  ,

	@OrderDate datetime   ,

	@CustomerId uniqueidentifier   ,

	@Popular decimal (18, 0)  ,

	@Deluxe decimal (18, 0)  ,

	@Note text   ,

	@Year smallint   ,

	@Month smallint   ,

	@BrandCode nvarchar (50)  
)
AS


					
				INSERT INTO [dbo].[Revenue]
					(
					[Order_Num]
					,[Order_Date]
					,[Customer_ID]
					,[Popular]
					,[Deluxe]
					,[Note]
					,[Year]
					,[Month]
					,[BrandCode]
					)
				VALUES
					(
					@OrderNum
					,@OrderDate
					,@CustomerId
					,@Popular
					,@Deluxe
					,@Note
					,@Year
					,@Month
					,@BrandCode
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the Revenue table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_Update
(

	@Id int   ,

	@OrderNum varchar (50)  ,

	@OrderDate datetime   ,

	@CustomerId uniqueidentifier   ,

	@Popular decimal (18, 0)  ,

	@Deluxe decimal (18, 0)  ,

	@Note text   ,

	@Year smallint   ,

	@Month smallint   ,

	@BrandCode nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Revenue]
				SET
					[Order_Num] = @OrderNum
					,[Order_Date] = @OrderDate
					,[Customer_ID] = @CustomerId
					,[Popular] = @Popular
					,[Deluxe] = @Deluxe
					,[Note] = @Note
					,[Year] = @Year
					,[Month] = @Month
					,[BrandCode] = @BrandCode
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the Revenue table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Revenue] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_GetByCustomerId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_GetByCustomerId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_GetByCustomerId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the Revenue table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_GetByCustomerId
(

	@CustomerId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ID],
					[Order_Num],
					[Order_Date],
					[Customer_ID],
					[Popular],
					[Deluxe],
					[Note],
					[Year],
					[Month],
					[BrandCode]
				FROM
					[dbo].[Revenue]
				WHERE
					[Customer_ID] = @CustomerId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the Revenue table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[Order_Num],
					[Order_Date],
					[Customer_ID],
					[Popular],
					[Deluxe],
					[Note],
					[Year],
					[Month],
					[BrandCode]
				FROM
					[dbo].[Revenue]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Revenue_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Revenue_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Revenue_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the Revenue table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Revenue_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@OrderNum varchar (50)  = null ,

	@OrderDate datetime   = null ,

	@CustomerId uniqueidentifier   = null ,

	@Popular decimal (18, 0)  = null ,

	@Deluxe decimal (18, 0)  = null ,

	@Note text   = null ,

	@Year smallint   = null ,

	@Month smallint   = null ,

	@BrandCode nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [Order_Num]
	, [Order_Date]
	, [Customer_ID]
	, [Popular]
	, [Deluxe]
	, [Note]
	, [Year]
	, [Month]
	, [BrandCode]
    FROM
	[dbo].[Revenue]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([Order_Num] = @OrderNum OR @OrderNum is null)
	AND ([Order_Date] = @OrderDate OR @OrderDate is null)
	AND ([Customer_ID] = @CustomerId OR @CustomerId is null)
	AND ([Popular] = @Popular OR @Popular is null)
	AND ([Deluxe] = @Deluxe OR @Deluxe is null)
	AND ([Year] = @Year OR @Year is null)
	AND ([Month] = @Month OR @Month is null)
	AND ([BrandCode] = @BrandCode OR @BrandCode is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [Order_Num]
	, [Order_Date]
	, [Customer_ID]
	, [Popular]
	, [Deluxe]
	, [Note]
	, [Year]
	, [Month]
	, [BrandCode]
    FROM
	[dbo].[Revenue]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([Order_Num] = @OrderNum AND @OrderNum is not null)
	OR ([Order_Date] = @OrderDate AND @OrderDate is not null)
	OR ([Customer_ID] = @CustomerId AND @CustomerId is not null)
	OR ([Popular] = @Popular AND @Popular is not null)
	OR ([Deluxe] = @Deluxe AND @Deluxe is not null)
	OR ([Year] = @Year AND @Year is not null)
	OR ([Month] = @Month AND @Month is not null)
	OR ([BrandCode] = @BrandCode AND @BrandCode is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the NhomKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_Get_List

AS


				
				SELECT
					[NhomKhachHangID],
					[KhachHangID],
					[LastUpdate],
					[UpdateBy],
					[Level]
				FROM
					[dbo].[NhomKhachHang]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the NhomKhachHang table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [NhomKhachHangID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([NhomKhachHangID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [NhomKhachHangID]'
				SET @SQL = @SQL + ' FROM [dbo].[NhomKhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[NhomKhachHangID], O.[KhachHangID], O.[LastUpdate], O.[UpdateBy], O.[Level]
				FROM
				    [dbo].[NhomKhachHang] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[NhomKhachHangID] = PageIndex.[NhomKhachHangID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[NhomKhachHang]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the NhomKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_Insert
(

	@NhomKhachHangId uniqueidentifier   ,

	@KhachHangId varchar (4000)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@Level smallint   
)
AS


					
				INSERT INTO [dbo].[NhomKhachHang]
					(
					[NhomKhachHangID]
					,[KhachHangID]
					,[LastUpdate]
					,[UpdateBy]
					,[Level]
					)
				VALUES
					(
					@NhomKhachHangId
					,@KhachHangId
					,@LastUpdate
					,@UpdateBy
					,@Level
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the NhomKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_Update
(

	@NhomKhachHangId uniqueidentifier   ,

	@OriginalNhomKhachHangId uniqueidentifier   ,

	@KhachHangId varchar (4000)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@Level smallint   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[NhomKhachHang]
				SET
					[NhomKhachHangID] = @NhomKhachHangId
					,[KhachHangID] = @KhachHangId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[Level] = @Level
				WHERE
[NhomKhachHangID] = @OriginalNhomKhachHangId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the NhomKhachHang table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_Delete
(

	@NhomKhachHangId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[NhomKhachHang] WITH (ROWLOCK) 
				WHERE
					[NhomKhachHangID] = @NhomKhachHangId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_GetByNhomKhachHangId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_GetByNhomKhachHangId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_GetByNhomKhachHangId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the NhomKhachHang table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_GetByNhomKhachHangId
(

	@NhomKhachHangId uniqueidentifier   
)
AS


				SELECT
					[NhomKhachHangID],
					[KhachHangID],
					[LastUpdate],
					[UpdateBy],
					[Level]
				FROM
					[dbo].[NhomKhachHang]
				WHERE
					[NhomKhachHangID] = @NhomKhachHangId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomKhachHang_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomKhachHang_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomKhachHang_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the NhomKhachHang table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomKhachHang_Find
(

	@SearchUsingOR bit   = null ,

	@NhomKhachHangId uniqueidentifier   = null ,

	@KhachHangId varchar (4000)  = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@Level smallint   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [NhomKhachHangID]
	, [KhachHangID]
	, [LastUpdate]
	, [UpdateBy]
	, [Level]
    FROM
	[dbo].[NhomKhachHang]
    WHERE 
	 ([NhomKhachHangID] = @NhomKhachHangId OR @NhomKhachHangId is null)
	AND ([KhachHangID] = @KhachHangId OR @KhachHangId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([Level] = @Level OR @Level is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [NhomKhachHangID]
	, [KhachHangID]
	, [LastUpdate]
	, [UpdateBy]
	, [Level]
    FROM
	[dbo].[NhomKhachHang]
    WHERE 
	 ([NhomKhachHangID] = @NhomKhachHangId AND @NhomKhachHangId is not null)
	OR ([KhachHangID] = @KhachHangId AND @KhachHangId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([Level] = @Level AND @Level is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the PhanThuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_Get_List

AS


				
				SELECT
					[ID],
					[Name],
					[Value],
					[Code],
					[IsActive]
				FROM
					[dbo].[PhanThuong]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the PhanThuong table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[PhanThuong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[Name], O.[Value], O.[Code], O.[IsActive]
				FROM
				    [dbo].[PhanThuong] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[PhanThuong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the PhanThuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_Insert
(

	@Id int    OUTPUT,

	@Name nvarchar (500)  ,

	@Value decimal (18, 0)  ,

	@Code nvarchar (50)  ,

	@IsActive bit   
)
AS


					
				INSERT INTO [dbo].[PhanThuong]
					(
					[Name]
					,[Value]
					,[Code]
					,[IsActive]
					)
				VALUES
					(
					@Name
					,@Value
					,@Code
					,@IsActive
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the PhanThuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_Update
(

	@Id int   ,

	@Name nvarchar (500)  ,

	@Value decimal (18, 0)  ,

	@Code nvarchar (50)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[PhanThuong]
				SET
					[Name] = @Name
					,[Value] = @Value
					,[Code] = @Code
					,[IsActive] = @IsActive
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the PhanThuong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[PhanThuong] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the PhanThuong table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[Name],
					[Value],
					[Code],
					[IsActive]
				FROM
					[dbo].[PhanThuong]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanThuong_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanThuong_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanThuong_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the PhanThuong table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanThuong_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Name nvarchar (500)  = null ,

	@Value decimal (18, 0)  = null ,

	@Code nvarchar (50)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [Name]
	, [Value]
	, [Code]
	, [IsActive]
    FROM
	[dbo].[PhanThuong]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([Name] = @Name OR @Name is null)
	AND ([Value] = @Value OR @Value is null)
	AND ([Code] = @Code OR @Code is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [Name]
	, [Value]
	, [Code]
	, [IsActive]
    FROM
	[dbo].[PhanThuong]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([Name] = @Name AND @Name is not null)
	OR ([Value] = @Value AND @Value is not null)
	OR ([Code] = @Code AND @Code is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the HoiVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_Get_List

AS


				
				SELECT
					[HoiVienID],
					[KhachHangID],
					[LoaiHoiVien],
					[MaSoHV],
					[NgayDangKy]
				FROM
					[dbo].[HoiVien]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the HoiVien table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [HoiVienID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([HoiVienID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [HoiVienID]'
				SET @SQL = @SQL + ' FROM [dbo].[HoiVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[HoiVienID], O.[KhachHangID], O.[LoaiHoiVien], O.[MaSoHV], O.[NgayDangKy]
				FROM
				    [dbo].[HoiVien] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[HoiVienID] = PageIndex.[HoiVienID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[HoiVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the HoiVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_Insert
(

	@HoiVienId uniqueidentifier   ,

	@KhachHangId uniqueidentifier   ,

	@LoaiHoiVien smallint   ,

	@MaSoHv varchar (20)  ,

	@NgayDangKy datetime   
)
AS


					
				INSERT INTO [dbo].[HoiVien]
					(
					[HoiVienID]
					,[KhachHangID]
					,[LoaiHoiVien]
					,[MaSoHV]
					,[NgayDangKy]
					)
				VALUES
					(
					@HoiVienId
					,@KhachHangId
					,@LoaiHoiVien
					,@MaSoHv
					,@NgayDangKy
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the HoiVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_Update
(

	@HoiVienId uniqueidentifier   ,

	@OriginalHoiVienId uniqueidentifier   ,

	@KhachHangId uniqueidentifier   ,

	@LoaiHoiVien smallint   ,

	@MaSoHv varchar (20)  ,

	@NgayDangKy datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[HoiVien]
				SET
					[HoiVienID] = @HoiVienId
					,[KhachHangID] = @KhachHangId
					,[LoaiHoiVien] = @LoaiHoiVien
					,[MaSoHV] = @MaSoHv
					,[NgayDangKy] = @NgayDangKy
				WHERE
[HoiVienID] = @OriginalHoiVienId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the HoiVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_Delete
(

	@HoiVienId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[HoiVien] WITH (ROWLOCK) 
				WHERE
					[HoiVienID] = @HoiVienId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_GetByHoiVienId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_GetByHoiVienId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_GetByHoiVienId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the HoiVien table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_GetByHoiVienId
(

	@HoiVienId uniqueidentifier   
)
AS


				SELECT
					[HoiVienID],
					[KhachHangID],
					[LoaiHoiVien],
					[MaSoHV],
					[NgayDangKy]
				FROM
					[dbo].[HoiVien]
				WHERE
					[HoiVienID] = @HoiVienId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoiVien_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoiVien_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoiVien_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the HoiVien table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoiVien_Find
(

	@SearchUsingOR bit   = null ,

	@HoiVienId uniqueidentifier   = null ,

	@KhachHangId uniqueidentifier   = null ,

	@LoaiHoiVien smallint   = null ,

	@MaSoHv varchar (20)  = null ,

	@NgayDangKy datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [HoiVienID]
	, [KhachHangID]
	, [LoaiHoiVien]
	, [MaSoHV]
	, [NgayDangKy]
    FROM
	[dbo].[HoiVien]
    WHERE 
	 ([HoiVienID] = @HoiVienId OR @HoiVienId is null)
	AND ([KhachHangID] = @KhachHangId OR @KhachHangId is null)
	AND ([LoaiHoiVien] = @LoaiHoiVien OR @LoaiHoiVien is null)
	AND ([MaSoHV] = @MaSoHv OR @MaSoHv is null)
	AND ([NgayDangKy] = @NgayDangKy OR @NgayDangKy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [HoiVienID]
	, [KhachHangID]
	, [LoaiHoiVien]
	, [MaSoHV]
	, [NgayDangKy]
    FROM
	[dbo].[HoiVien]
    WHERE 
	 ([HoiVienID] = @HoiVienId AND @HoiVienId is not null)
	OR ([KhachHangID] = @KhachHangId AND @KhachHangId is not null)
	OR ([LoaiHoiVien] = @LoaiHoiVien AND @LoaiHoiVien is not null)
	OR ([MaSoHV] = @MaSoHv AND @MaSoHv is not null)
	OR ([NgayDangKy] = @NgayDangKy AND @NgayDangKy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the NhomSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_Get_List

AS


				
				SELECT
					[NhomSanPhamID],
					[SanPhamID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[NhomSanPham]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the NhomSanPham table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [NhomSanPhamID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([NhomSanPhamID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [NhomSanPhamID]'
				SET @SQL = @SQL + ' FROM [dbo].[NhomSanPham]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[NhomSanPhamID], O.[SanPhamID], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[NhomSanPham] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[NhomSanPhamID] = PageIndex.[NhomSanPhamID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[NhomSanPham]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the NhomSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_Insert
(

	@NhomSanPhamId uniqueidentifier   ,

	@SanPhamId varchar (4000)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[NhomSanPham]
					(
					[NhomSanPhamID]
					,[SanPhamID]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@NhomSanPhamId
					,@SanPhamId
					,@LastUpdate
					,@UpdateBy
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the NhomSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_Update
(

	@NhomSanPhamId uniqueidentifier   ,

	@OriginalNhomSanPhamId uniqueidentifier   ,

	@SanPhamId varchar (4000)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[NhomSanPham]
				SET
					[NhomSanPhamID] = @NhomSanPhamId
					,[SanPhamID] = @SanPhamId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[NhomSanPhamID] = @OriginalNhomSanPhamId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the NhomSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_Delete
(

	@NhomSanPhamId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[NhomSanPham] WITH (ROWLOCK) 
				WHERE
					[NhomSanPhamID] = @NhomSanPhamId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_GetByNhomSanPhamId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_GetByNhomSanPhamId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_GetByNhomSanPhamId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the NhomSanPham table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_GetByNhomSanPhamId
(

	@NhomSanPhamId uniqueidentifier   
)
AS


				SELECT
					[NhomSanPhamID],
					[SanPhamID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[NhomSanPham]
				WHERE
					[NhomSanPhamID] = @NhomSanPhamId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhomSanPham_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhomSanPham_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhomSanPham_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the NhomSanPham table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhomSanPham_Find
(

	@SearchUsingOR bit   = null ,

	@NhomSanPhamId uniqueidentifier   = null ,

	@SanPhamId varchar (4000)  = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [NhomSanPhamID]
	, [SanPhamID]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[NhomSanPham]
    WHERE 
	 ([NhomSanPhamID] = @NhomSanPhamId OR @NhomSanPhamId is null)
	AND ([SanPhamID] = @SanPhamId OR @SanPhamId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [NhomSanPhamID]
	, [SanPhamID]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[NhomSanPham]
    WHERE 
	 ([NhomSanPhamID] = @NhomSanPhamId AND @NhomSanPhamId is not null)
	OR ([SanPhamID] = @SanPhamId AND @SanPhamId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the DonViTinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_Get_List

AS


				
				SELECT
					[DonViTinhID],
					[TenDonViTinh],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[DonViTinh]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the DonViTinh table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [DonViTinhID] tinyint 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([DonViTinhID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [DonViTinhID]'
				SET @SQL = @SQL + ' FROM [dbo].[DonViTinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[DonViTinhID], O.[TenDonViTinh], O.[IsActive], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[DonViTinh] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[DonViTinhID] = PageIndex.[DonViTinhID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DonViTinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the DonViTinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_Insert
(

	@DonViTinhId tinyint    OUTPUT,

	@TenDonViTinh nvarchar (50)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[DonViTinh]
					(
					[TenDonViTinh]
					,[IsActive]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@TenDonViTinh
					,@IsActive
					,@LastUpdate
					,@UpdateBy
					)
				
				-- Get the identity value
				SET @DonViTinhId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the DonViTinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_Update
(

	@DonViTinhId tinyint   ,

	@TenDonViTinh nvarchar (50)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DonViTinh]
				SET
					[TenDonViTinh] = @TenDonViTinh
					,[IsActive] = @IsActive
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[DonViTinhID] = @DonViTinhId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the DonViTinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_Delete
(

	@DonViTinhId tinyint   
)
AS


				DELETE FROM [dbo].[DonViTinh] WITH (ROWLOCK) 
				WHERE
					[DonViTinhID] = @DonViTinhId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_GetByDonViTinhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_GetByDonViTinhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_GetByDonViTinhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the DonViTinh table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_GetByDonViTinhId
(

	@DonViTinhId tinyint   
)
AS


				SELECT
					[DonViTinhID],
					[TenDonViTinh],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[DonViTinh]
				WHERE
					[DonViTinhID] = @DonViTinhId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DonViTinh_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DonViTinh_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DonViTinh_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the DonViTinh table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DonViTinh_Find
(

	@SearchUsingOR bit   = null ,

	@DonViTinhId tinyint   = null ,

	@TenDonViTinh nvarchar (50)  = null ,

	@IsActive bit   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [DonViTinhID]
	, [TenDonViTinh]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[DonViTinh]
    WHERE 
	 ([DonViTinhID] = @DonViTinhId OR @DonViTinhId is null)
	AND ([TenDonViTinh] = @TenDonViTinh OR @TenDonViTinh is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [DonViTinhID]
	, [TenDonViTinh]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[DonViTinh]
    WHERE 
	 ([DonViTinhID] = @DonViTinhId AND @DonViTinhId is not null)
	OR ([TenDonViTinh] = @TenDonViTinh AND @TenDonViTinh is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChietKhauSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_Get_List

AS


				
				SELECT
					[ChietKhauSPID],
					[NhomSanPhamID],
					[DonViTinhID],
					[SoLuong],
					[MucThuong],
					[LoaiThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[HinhThucThuong]
				FROM
					[dbo].[ChietKhauSanPham]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChietKhauSanPham table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChietKhauSPID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChietKhauSPID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChietKhauSPID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauSanPham]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChietKhauSPID], O.[NhomSanPhamID], O.[DonViTinhID], O.[SoLuong], O.[MucThuong], O.[LoaiThuong], O.[ChietKhauID], O.[LastUpdate], O.[UpdateBy], O.[HinhThucThuong]
				FROM
				    [dbo].[ChietKhauSanPham] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChietKhauSPID] = PageIndex.[ChietKhauSPID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauSanPham]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChietKhauSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_Insert
(

	@ChietKhauSpid uniqueidentifier   ,

	@NhomSanPhamId uniqueidentifier   ,

	@DonViTinhId tinyint   ,

	@SoLuong tinyint   ,

	@MucThuong float   ,

	@LoaiThuong bit   ,

	@ChietKhauId uniqueidentifier   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@HinhThucThuong nvarchar (50)  
)
AS


					
				INSERT INTO [dbo].[ChietKhauSanPham]
					(
					[ChietKhauSPID]
					,[NhomSanPhamID]
					,[DonViTinhID]
					,[SoLuong]
					,[MucThuong]
					,[LoaiThuong]
					,[ChietKhauID]
					,[LastUpdate]
					,[UpdateBy]
					,[HinhThucThuong]
					)
				VALUES
					(
					@ChietKhauSpid
					,@NhomSanPhamId
					,@DonViTinhId
					,@SoLuong
					,@MucThuong
					,@LoaiThuong
					,@ChietKhauId
					,@LastUpdate
					,@UpdateBy
					,@HinhThucThuong
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChietKhauSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_Update
(

	@ChietKhauSpid uniqueidentifier   ,

	@OriginalChietKhauSpid uniqueidentifier   ,

	@NhomSanPhamId uniqueidentifier   ,

	@DonViTinhId tinyint   ,

	@SoLuong tinyint   ,

	@MucThuong float   ,

	@LoaiThuong bit   ,

	@ChietKhauId uniqueidentifier   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@HinhThucThuong nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChietKhauSanPham]
				SET
					[ChietKhauSPID] = @ChietKhauSpid
					,[NhomSanPhamID] = @NhomSanPhamId
					,[DonViTinhID] = @DonViTinhId
					,[SoLuong] = @SoLuong
					,[MucThuong] = @MucThuong
					,[LoaiThuong] = @LoaiThuong
					,[ChietKhauID] = @ChietKhauId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[HinhThucThuong] = @HinhThucThuong
				WHERE
[ChietKhauSPID] = @OriginalChietKhauSpid 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChietKhauSanPham table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_Delete
(

	@ChietKhauSpid uniqueidentifier   
)
AS


				DELETE FROM [dbo].[ChietKhauSanPham] WITH (ROWLOCK) 
				WHERE
					[ChietKhauSPID] = @ChietKhauSpid
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_GetByNhomSanPhamId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_GetByNhomSanPhamId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_GetByNhomSanPhamId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauSanPham table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_GetByNhomSanPhamId
(

	@NhomSanPhamId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauSPID],
					[NhomSanPhamID],
					[DonViTinhID],
					[SoLuong],
					[MucThuong],
					[LoaiThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[HinhThucThuong]
				FROM
					[dbo].[ChietKhauSanPham]
				WHERE
					[NhomSanPhamID] = @NhomSanPhamId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_GetByChietKhauId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_GetByChietKhauId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_GetByChietKhauId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauSanPham table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_GetByChietKhauId
(

	@ChietKhauId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauSPID],
					[NhomSanPhamID],
					[DonViTinhID],
					[SoLuong],
					[MucThuong],
					[LoaiThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[HinhThucThuong]
				FROM
					[dbo].[ChietKhauSanPham]
				WHERE
					[ChietKhauID] = @ChietKhauId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_GetByDonViTinhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_GetByDonViTinhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_GetByDonViTinhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauSanPham table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_GetByDonViTinhId
(

	@DonViTinhId tinyint   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauSPID],
					[NhomSanPhamID],
					[DonViTinhID],
					[SoLuong],
					[MucThuong],
					[LoaiThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[HinhThucThuong]
				FROM
					[dbo].[ChietKhauSanPham]
				WHERE
					[DonViTinhID] = @DonViTinhId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_GetByChietKhauSpid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_GetByChietKhauSpid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_GetByChietKhauSpid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauSanPham table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_GetByChietKhauSpid
(

	@ChietKhauSpid uniqueidentifier   
)
AS


				SELECT
					[ChietKhauSPID],
					[NhomSanPhamID],
					[DonViTinhID],
					[SoLuong],
					[MucThuong],
					[LoaiThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[HinhThucThuong]
				FROM
					[dbo].[ChietKhauSanPham]
				WHERE
					[ChietKhauSPID] = @ChietKhauSpid
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauSanPham_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauSanPham_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauSanPham_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChietKhauSanPham table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauSanPham_Find
(

	@SearchUsingOR bit   = null ,

	@ChietKhauSpid uniqueidentifier   = null ,

	@NhomSanPhamId uniqueidentifier   = null ,

	@DonViTinhId tinyint   = null ,

	@SoLuong tinyint   = null ,

	@MucThuong float   = null ,

	@LoaiThuong bit   = null ,

	@ChietKhauId uniqueidentifier   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@HinhThucThuong nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChietKhauSPID]
	, [NhomSanPhamID]
	, [DonViTinhID]
	, [SoLuong]
	, [MucThuong]
	, [LoaiThuong]
	, [ChietKhauID]
	, [LastUpdate]
	, [UpdateBy]
	, [HinhThucThuong]
    FROM
	[dbo].[ChietKhauSanPham]
    WHERE 
	 ([ChietKhauSPID] = @ChietKhauSpid OR @ChietKhauSpid is null)
	AND ([NhomSanPhamID] = @NhomSanPhamId OR @NhomSanPhamId is null)
	AND ([DonViTinhID] = @DonViTinhId OR @DonViTinhId is null)
	AND ([SoLuong] = @SoLuong OR @SoLuong is null)
	AND ([MucThuong] = @MucThuong OR @MucThuong is null)
	AND ([LoaiThuong] = @LoaiThuong OR @LoaiThuong is null)
	AND ([ChietKhauID] = @ChietKhauId OR @ChietKhauId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([HinhThucThuong] = @HinhThucThuong OR @HinhThucThuong is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChietKhauSPID]
	, [NhomSanPhamID]
	, [DonViTinhID]
	, [SoLuong]
	, [MucThuong]
	, [LoaiThuong]
	, [ChietKhauID]
	, [LastUpdate]
	, [UpdateBy]
	, [HinhThucThuong]
    FROM
	[dbo].[ChietKhauSanPham]
    WHERE 
	 ([ChietKhauSPID] = @ChietKhauSpid AND @ChietKhauSpid is not null)
	OR ([NhomSanPhamID] = @NhomSanPhamId AND @NhomSanPhamId is not null)
	OR ([DonViTinhID] = @DonViTinhId AND @DonViTinhId is not null)
	OR ([SoLuong] = @SoLuong AND @SoLuong is not null)
	OR ([MucThuong] = @MucThuong AND @MucThuong is not null)
	OR ([LoaiThuong] = @LoaiThuong AND @LoaiThuong is not null)
	OR ([ChietKhauID] = @ChietKhauId AND @ChietKhauId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([HinhThucThuong] = @HinhThucThuong AND @HinhThucThuong is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChietKhauNhanh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_Get_List

AS


				
				SELECT
					[ChietKhauNhanhID],
					[MucThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[NhomSanPhamID]
				FROM
					[dbo].[ChietKhauNhanh]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChietKhauNhanh table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChietKhauNhanhID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChietKhauNhanhID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChietKhauNhanhID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauNhanh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChietKhauNhanhID], O.[MucThuong], O.[ChietKhauID], O.[LastUpdate], O.[UpdateBy], O.[NhomSanPhamID]
				FROM
				    [dbo].[ChietKhauNhanh] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChietKhauNhanhID] = PageIndex.[ChietKhauNhanhID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauNhanh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChietKhauNhanh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_Insert
(

	@ChietKhauNhanhId uniqueidentifier   ,

	@MucThuong tinyint   ,

	@ChietKhauId uniqueidentifier   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@NhomSanPhamId uniqueidentifier   
)
AS


					
				INSERT INTO [dbo].[ChietKhauNhanh]
					(
					[ChietKhauNhanhID]
					,[MucThuong]
					,[ChietKhauID]
					,[LastUpdate]
					,[UpdateBy]
					,[NhomSanPhamID]
					)
				VALUES
					(
					@ChietKhauNhanhId
					,@MucThuong
					,@ChietKhauId
					,@LastUpdate
					,@UpdateBy
					,@NhomSanPhamId
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChietKhauNhanh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_Update
(

	@ChietKhauNhanhId uniqueidentifier   ,

	@OriginalChietKhauNhanhId uniqueidentifier   ,

	@MucThuong tinyint   ,

	@ChietKhauId uniqueidentifier   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@NhomSanPhamId uniqueidentifier   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChietKhauNhanh]
				SET
					[ChietKhauNhanhID] = @ChietKhauNhanhId
					,[MucThuong] = @MucThuong
					,[ChietKhauID] = @ChietKhauId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[NhomSanPhamID] = @NhomSanPhamId
				WHERE
[ChietKhauNhanhID] = @OriginalChietKhauNhanhId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChietKhauNhanh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_Delete
(

	@ChietKhauNhanhId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[ChietKhauNhanh] WITH (ROWLOCK) 
				WHERE
					[ChietKhauNhanhID] = @ChietKhauNhanhId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_GetByNhomSanPhamId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_GetByNhomSanPhamId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_GetByNhomSanPhamId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauNhanh table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_GetByNhomSanPhamId
(

	@NhomSanPhamId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauNhanhID],
					[MucThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[NhomSanPhamID]
				FROM
					[dbo].[ChietKhauNhanh]
				WHERE
					[NhomSanPhamID] = @NhomSanPhamId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_GetByChietKhauId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_GetByChietKhauId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_GetByChietKhauId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauNhanh table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_GetByChietKhauId
(

	@ChietKhauId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauNhanhID],
					[MucThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[NhomSanPhamID]
				FROM
					[dbo].[ChietKhauNhanh]
				WHERE
					[ChietKhauID] = @ChietKhauId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_GetByChietKhauNhanhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_GetByChietKhauNhanhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_GetByChietKhauNhanhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauNhanh table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_GetByChietKhauNhanhId
(

	@ChietKhauNhanhId uniqueidentifier   
)
AS


				SELECT
					[ChietKhauNhanhID],
					[MucThuong],
					[ChietKhauID],
					[LastUpdate],
					[UpdateBy],
					[NhomSanPhamID]
				FROM
					[dbo].[ChietKhauNhanh]
				WHERE
					[ChietKhauNhanhID] = @ChietKhauNhanhId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauNhanh_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauNhanh_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauNhanh_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChietKhauNhanh table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauNhanh_Find
(

	@SearchUsingOR bit   = null ,

	@ChietKhauNhanhId uniqueidentifier   = null ,

	@MucThuong tinyint   = null ,

	@ChietKhauId uniqueidentifier   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@NhomSanPhamId uniqueidentifier   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChietKhauNhanhID]
	, [MucThuong]
	, [ChietKhauID]
	, [LastUpdate]
	, [UpdateBy]
	, [NhomSanPhamID]
    FROM
	[dbo].[ChietKhauNhanh]
    WHERE 
	 ([ChietKhauNhanhID] = @ChietKhauNhanhId OR @ChietKhauNhanhId is null)
	AND ([MucThuong] = @MucThuong OR @MucThuong is null)
	AND ([ChietKhauID] = @ChietKhauId OR @ChietKhauId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([NhomSanPhamID] = @NhomSanPhamId OR @NhomSanPhamId is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChietKhauNhanhID]
	, [MucThuong]
	, [ChietKhauID]
	, [LastUpdate]
	, [UpdateBy]
	, [NhomSanPhamID]
    FROM
	[dbo].[ChietKhauNhanh]
    WHERE 
	 ([ChietKhauNhanhID] = @ChietKhauNhanhId AND @ChietKhauNhanhId is not null)
	OR ([MucThuong] = @MucThuong AND @MucThuong is not null)
	OR ([ChietKhauID] = @ChietKhauId AND @ChietKhauId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([NhomSanPhamID] = @NhomSanPhamId AND @NhomSanPhamId is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChuongTrinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_Get_List

AS


				
				SELECT
					[ChuongTrinhID],
					[TenChuongTrinh],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinh]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChuongTrinh table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChuongTrinhID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChuongTrinhID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChuongTrinhID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChuongTrinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChuongTrinhID], O.[TenChuongTrinh], O.[IsActive], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[ChuongTrinh] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChuongTrinhID] = PageIndex.[ChuongTrinhID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChuongTrinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChuongTrinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_Insert
(

	@ChuongTrinhId int    OUTPUT,

	@TenChuongTrinh nvarchar (150)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[ChuongTrinh]
					(
					[TenChuongTrinh]
					,[IsActive]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@TenChuongTrinh
					,@IsActive
					,@LastUpdate
					,@UpdateBy
					)
				
				-- Get the identity value
				SET @ChuongTrinhId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChuongTrinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_Update
(

	@ChuongTrinhId int   ,

	@TenChuongTrinh nvarchar (150)  ,

	@IsActive bit   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChuongTrinh]
				SET
					[TenChuongTrinh] = @TenChuongTrinh
					,[IsActive] = @IsActive
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[ChuongTrinhID] = @ChuongTrinhId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChuongTrinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_Delete
(

	@ChuongTrinhId int   
)
AS


				DELETE FROM [dbo].[ChuongTrinh] WITH (ROWLOCK) 
				WHERE
					[ChuongTrinhID] = @ChuongTrinhId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_GetByChuongTrinhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_GetByChuongTrinhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_GetByChuongTrinhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChuongTrinh table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_GetByChuongTrinhId
(

	@ChuongTrinhId int   
)
AS


				SELECT
					[ChuongTrinhID],
					[TenChuongTrinh],
					[IsActive],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinh]
				WHERE
					[ChuongTrinhID] = @ChuongTrinhId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinh_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinh_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinh_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChuongTrinh table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinh_Find
(

	@SearchUsingOR bit   = null ,

	@ChuongTrinhId int   = null ,

	@TenChuongTrinh nvarchar (150)  = null ,

	@IsActive bit   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChuongTrinhID]
	, [TenChuongTrinh]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[ChuongTrinh]
    WHERE 
	 ([ChuongTrinhID] = @ChuongTrinhId OR @ChuongTrinhId is null)
	AND ([TenChuongTrinh] = @TenChuongTrinh OR @TenChuongTrinh is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChuongTrinhID]
	, [TenChuongTrinh]
	, [IsActive]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[ChuongTrinh]
    WHERE 
	 ([ChuongTrinhID] = @ChuongTrinhId AND @ChuongTrinhId is not null)
	OR ([TenChuongTrinh] = @TenChuongTrinh AND @TenChuongTrinh is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChietKhauDoanhSo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_Get_List

AS


				
				SELECT
					[ChietKhauDSID],
					[DoanhSo],
					[MucThuong],
					[ChietKhauID],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy],
					[LoaiThoiGianID],
					[NhomSanPhamID],
					[ToanTuSoSanh]
				FROM
					[dbo].[ChietKhauDoanhSo]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChietKhauDoanhSo table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChietKhauDSID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChietKhauDSID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChietKhauDSID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauDoanhSo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChietKhauDSID], O.[DoanhSo], O.[MucThuong], O.[ChietKhauID], O.[ChuongTrinhID], O.[LastUpdate], O.[UpdateBy], O.[LoaiThoiGianID], O.[NhomSanPhamID], O.[ToanTuSoSanh]
				FROM
				    [dbo].[ChietKhauDoanhSo] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChietKhauDSID] = PageIndex.[ChietKhauDSID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChietKhauDoanhSo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChietKhauDoanhSo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_Insert
(

	@ChietKhauDsid uniqueidentifier   ,

	@DoanhSo int   ,

	@MucThuong float   ,

	@ChietKhauId uniqueidentifier   ,

	@ChuongTrinhId int   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@LoaiThoiGianId tinyint   ,

	@NhomSanPhamId uniqueidentifier   ,

	@ToanTuSoSanh varchar (2)  
)
AS


					
				INSERT INTO [dbo].[ChietKhauDoanhSo]
					(
					[ChietKhauDSID]
					,[DoanhSo]
					,[MucThuong]
					,[ChietKhauID]
					,[ChuongTrinhID]
					,[LastUpdate]
					,[UpdateBy]
					,[LoaiThoiGianID]
					,[NhomSanPhamID]
					,[ToanTuSoSanh]
					)
				VALUES
					(
					@ChietKhauDsid
					,@DoanhSo
					,@MucThuong
					,@ChietKhauId
					,@ChuongTrinhId
					,@LastUpdate
					,@UpdateBy
					,@LoaiThoiGianId
					,@NhomSanPhamId
					,@ToanTuSoSanh
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChietKhauDoanhSo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_Update
(

	@ChietKhauDsid uniqueidentifier   ,

	@OriginalChietKhauDsid uniqueidentifier   ,

	@DoanhSo int   ,

	@MucThuong float   ,

	@ChietKhauId uniqueidentifier   ,

	@ChuongTrinhId int   ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@LoaiThoiGianId tinyint   ,

	@NhomSanPhamId uniqueidentifier   ,

	@ToanTuSoSanh varchar (2)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChietKhauDoanhSo]
				SET
					[ChietKhauDSID] = @ChietKhauDsid
					,[DoanhSo] = @DoanhSo
					,[MucThuong] = @MucThuong
					,[ChietKhauID] = @ChietKhauId
					,[ChuongTrinhID] = @ChuongTrinhId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[LoaiThoiGianID] = @LoaiThoiGianId
					,[NhomSanPhamID] = @NhomSanPhamId
					,[ToanTuSoSanh] = @ToanTuSoSanh
				WHERE
[ChietKhauDSID] = @OriginalChietKhauDsid 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChietKhauDoanhSo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_Delete
(

	@ChietKhauDsid uniqueidentifier   
)
AS


				DELETE FROM [dbo].[ChietKhauDoanhSo] WITH (ROWLOCK) 
				WHERE
					[ChietKhauDSID] = @ChietKhauDsid
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_GetByNhomSanPhamId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_GetByNhomSanPhamId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_GetByNhomSanPhamId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauDoanhSo table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_GetByNhomSanPhamId
(

	@NhomSanPhamId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauDSID],
					[DoanhSo],
					[MucThuong],
					[ChietKhauID],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy],
					[LoaiThoiGianID],
					[NhomSanPhamID],
					[ToanTuSoSanh]
				FROM
					[dbo].[ChietKhauDoanhSo]
				WHERE
					[NhomSanPhamID] = @NhomSanPhamId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_GetByChietKhauId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_GetByChietKhauId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_GetByChietKhauId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauDoanhSo table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_GetByChietKhauId
(

	@ChietKhauId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauDSID],
					[DoanhSo],
					[MucThuong],
					[ChietKhauID],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy],
					[LoaiThoiGianID],
					[NhomSanPhamID],
					[ToanTuSoSanh]
				FROM
					[dbo].[ChietKhauDoanhSo]
				WHERE
					[ChietKhauID] = @ChietKhauId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_GetByLoaiThoiGianId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_GetByLoaiThoiGianId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_GetByLoaiThoiGianId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauDoanhSo table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_GetByLoaiThoiGianId
(

	@LoaiThoiGianId tinyint   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChietKhauDSID],
					[DoanhSo],
					[MucThuong],
					[ChietKhauID],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy],
					[LoaiThoiGianID],
					[NhomSanPhamID],
					[ToanTuSoSanh]
				FROM
					[dbo].[ChietKhauDoanhSo]
				WHERE
					[LoaiThoiGianID] = @LoaiThoiGianId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_GetByChietKhauDsid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_GetByChietKhauDsid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_GetByChietKhauDsid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChietKhauDoanhSo table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_GetByChietKhauDsid
(

	@ChietKhauDsid uniqueidentifier   
)
AS


				SELECT
					[ChietKhauDSID],
					[DoanhSo],
					[MucThuong],
					[ChietKhauID],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy],
					[LoaiThoiGianID],
					[NhomSanPhamID],
					[ToanTuSoSanh]
				FROM
					[dbo].[ChietKhauDoanhSo]
				WHERE
					[ChietKhauDSID] = @ChietKhauDsid
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChietKhauDoanhSo_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChietKhauDoanhSo_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChietKhauDoanhSo_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChietKhauDoanhSo table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChietKhauDoanhSo_Find
(

	@SearchUsingOR bit   = null ,

	@ChietKhauDsid uniqueidentifier   = null ,

	@DoanhSo int   = null ,

	@MucThuong float   = null ,

	@ChietKhauId uniqueidentifier   = null ,

	@ChuongTrinhId int   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@LoaiThoiGianId tinyint   = null ,

	@NhomSanPhamId uniqueidentifier   = null ,

	@ToanTuSoSanh varchar (2)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChietKhauDSID]
	, [DoanhSo]
	, [MucThuong]
	, [ChietKhauID]
	, [ChuongTrinhID]
	, [LastUpdate]
	, [UpdateBy]
	, [LoaiThoiGianID]
	, [NhomSanPhamID]
	, [ToanTuSoSanh]
    FROM
	[dbo].[ChietKhauDoanhSo]
    WHERE 
	 ([ChietKhauDSID] = @ChietKhauDsid OR @ChietKhauDsid is null)
	AND ([DoanhSo] = @DoanhSo OR @DoanhSo is null)
	AND ([MucThuong] = @MucThuong OR @MucThuong is null)
	AND ([ChietKhauID] = @ChietKhauId OR @ChietKhauId is null)
	AND ([ChuongTrinhID] = @ChuongTrinhId OR @ChuongTrinhId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([LoaiThoiGianID] = @LoaiThoiGianId OR @LoaiThoiGianId is null)
	AND ([NhomSanPhamID] = @NhomSanPhamId OR @NhomSanPhamId is null)
	AND ([ToanTuSoSanh] = @ToanTuSoSanh OR @ToanTuSoSanh is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChietKhauDSID]
	, [DoanhSo]
	, [MucThuong]
	, [ChietKhauID]
	, [ChuongTrinhID]
	, [LastUpdate]
	, [UpdateBy]
	, [LoaiThoiGianID]
	, [NhomSanPhamID]
	, [ToanTuSoSanh]
    FROM
	[dbo].[ChietKhauDoanhSo]
    WHERE 
	 ([ChietKhauDSID] = @ChietKhauDsid AND @ChietKhauDsid is not null)
	OR ([DoanhSo] = @DoanhSo AND @DoanhSo is not null)
	OR ([MucThuong] = @MucThuong AND @MucThuong is not null)
	OR ([ChietKhauID] = @ChietKhauId AND @ChietKhauId is not null)
	OR ([ChuongTrinhID] = @ChuongTrinhId AND @ChuongTrinhId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([LoaiThoiGianID] = @LoaiThoiGianId AND @LoaiThoiGianId is not null)
	OR ([NhomSanPhamID] = @NhomSanPhamId AND @NhomSanPhamId is not null)
	OR ([ToanTuSoSanh] = @ToanTuSoSanh AND @ToanTuSoSanh is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ChuongTrinhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_Get_List

AS


				
				SELECT
					[ChiTietID],
					[LoaiThoiGianID],
					[SoThoiGian],
					[DoanhSo],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinhChiTiet]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ChuongTrinhChiTiet table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ChiTietID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ChiTietID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ChiTietID]'
				SET @SQL = @SQL + ' FROM [dbo].[ChuongTrinhChiTiet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ChiTietID], O.[LoaiThoiGianID], O.[SoThoiGian], O.[DoanhSo], O.[ChuongTrinhID], O.[LastUpdate], O.[UpdateBy]
				FROM
				    [dbo].[ChuongTrinhChiTiet] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ChiTietID] = PageIndex.[ChiTietID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChuongTrinhChiTiet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ChuongTrinhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_Insert
(

	@ChiTietId int    OUTPUT,

	@LoaiThoiGianId tinyint   ,

	@SoThoiGian tinyint   ,

	@DoanhSo int   ,

	@ChuongTrinhId int   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


					
				INSERT INTO [dbo].[ChuongTrinhChiTiet]
					(
					[LoaiThoiGianID]
					,[SoThoiGian]
					,[DoanhSo]
					,[ChuongTrinhID]
					,[LastUpdate]
					,[UpdateBy]
					)
				VALUES
					(
					@LoaiThoiGianId
					,@SoThoiGian
					,@DoanhSo
					,@ChuongTrinhId
					,@LastUpdate
					,@UpdateBy
					)
				
				-- Get the identity value
				SET @ChiTietId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ChuongTrinhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_Update
(

	@ChiTietId int   ,

	@LoaiThoiGianId tinyint   ,

	@SoThoiGian tinyint   ,

	@DoanhSo int   ,

	@ChuongTrinhId int   ,

	@LastUpdate datetime   ,

	@UpdateBy int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChuongTrinhChiTiet]
				SET
					[LoaiThoiGianID] = @LoaiThoiGianId
					,[SoThoiGian] = @SoThoiGian
					,[DoanhSo] = @DoanhSo
					,[ChuongTrinhID] = @ChuongTrinhId
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
				WHERE
[ChiTietID] = @ChiTietId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ChuongTrinhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_Delete
(

	@ChiTietId int   
)
AS


				DELETE FROM [dbo].[ChuongTrinhChiTiet] WITH (ROWLOCK) 
				WHERE
					[ChiTietID] = @ChiTietId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_GetByChuongTrinhId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_GetByChuongTrinhId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_GetByChuongTrinhId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChuongTrinhChiTiet table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_GetByChuongTrinhId
(

	@ChuongTrinhId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChiTietID],
					[LoaiThoiGianID],
					[SoThoiGian],
					[DoanhSo],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinhChiTiet]
				WHERE
					[ChuongTrinhID] = @ChuongTrinhId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_GetByLoaiThoiGianId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_GetByLoaiThoiGianId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_GetByLoaiThoiGianId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChuongTrinhChiTiet table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_GetByLoaiThoiGianId
(

	@LoaiThoiGianId tinyint   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ChiTietID],
					[LoaiThoiGianID],
					[SoThoiGian],
					[DoanhSo],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinhChiTiet]
				WHERE
					[LoaiThoiGianID] = @LoaiThoiGianId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_GetByChiTietId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_GetByChiTietId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_GetByChiTietId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ChuongTrinhChiTiet table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_GetByChiTietId
(

	@ChiTietId int   
)
AS


				SELECT
					[ChiTietID],
					[LoaiThoiGianID],
					[SoThoiGian],
					[DoanhSo],
					[ChuongTrinhID],
					[LastUpdate],
					[UpdateBy]
				FROM
					[dbo].[ChuongTrinhChiTiet]
				WHERE
					[ChiTietID] = @ChiTietId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuongTrinhChiTiet_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuongTrinhChiTiet_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuongTrinhChiTiet_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ChuongTrinhChiTiet table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuongTrinhChiTiet_Find
(

	@SearchUsingOR bit   = null ,

	@ChiTietId int   = null ,

	@LoaiThoiGianId tinyint   = null ,

	@SoThoiGian tinyint   = null ,

	@DoanhSo int   = null ,

	@ChuongTrinhId int   = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ChiTietID]
	, [LoaiThoiGianID]
	, [SoThoiGian]
	, [DoanhSo]
	, [ChuongTrinhID]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[ChuongTrinhChiTiet]
    WHERE 
	 ([ChiTietID] = @ChiTietId OR @ChiTietId is null)
	AND ([LoaiThoiGianID] = @LoaiThoiGianId OR @LoaiThoiGianId is null)
	AND ([SoThoiGian] = @SoThoiGian OR @SoThoiGian is null)
	AND ([DoanhSo] = @DoanhSo OR @DoanhSo is null)
	AND ([ChuongTrinhID] = @ChuongTrinhId OR @ChuongTrinhId is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ChiTietID]
	, [LoaiThoiGianID]
	, [SoThoiGian]
	, [DoanhSo]
	, [ChuongTrinhID]
	, [LastUpdate]
	, [UpdateBy]
    FROM
	[dbo].[ChuongTrinhChiTiet]
    WHERE 
	 ([ChiTietID] = @ChiTietId AND @ChiTietId is not null)
	OR ([LoaiThoiGianID] = @LoaiThoiGianId AND @LoaiThoiGianId is not null)
	OR ([SoThoiGian] = @SoThoiGian AND @SoThoiGian is not null)
	OR ([DoanhSo] = @DoanhSo AND @DoanhSo is not null)
	OR ([ChuongTrinhID] = @ChuongTrinhId AND @ChuongTrinhId is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the DoanhSoCamKetLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Get_List

AS


				
				SELECT
					[ID],
					[KhachHangDuThuongID],
					[Year],
					[Quarter],
					[Month],
					[DoanhSoBase],
					[DoanhSoBotBa],
					[DoanhSoSon],
					[IsActive],
					[UpdatedDate],
					[UpdatedBy]
				FROM
					[dbo].[DoanhSoCamKetLapMayPhaMau]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the DoanhSoCamKetLapMayPhaMau table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[DoanhSoCamKetLapMayPhaMau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[KhachHangDuThuongID], O.[Year], O.[Quarter], O.[Month], O.[DoanhSoBase], O.[DoanhSoBotBa], O.[DoanhSoSon], O.[IsActive], O.[UpdatedDate], O.[UpdatedBy]
				FROM
				    [dbo].[DoanhSoCamKetLapMayPhaMau] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DoanhSoCamKetLapMayPhaMau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the DoanhSoCamKetLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Insert
(

	@Id int    OUTPUT,

	@KhachHangDuThuongId uniqueidentifier   ,

	@Year int   ,

	@Quarter int   ,

	@Month int   ,

	@DoanhSoBase decimal (18, 0)  ,

	@DoanhSoBotBa decimal (18, 0)  ,

	@DoanhSoSon decimal (18, 0)  ,

	@IsActive bit   ,

	@UpdatedDate datetime   ,

	@UpdatedBy nvarchar (50)  
)
AS


					
				INSERT INTO [dbo].[DoanhSoCamKetLapMayPhaMau]
					(
					[KhachHangDuThuongID]
					,[Year]
					,[Quarter]
					,[Month]
					,[DoanhSoBase]
					,[DoanhSoBotBa]
					,[DoanhSoSon]
					,[IsActive]
					,[UpdatedDate]
					,[UpdatedBy]
					)
				VALUES
					(
					@KhachHangDuThuongId
					,@Year
					,@Quarter
					,@Month
					,@DoanhSoBase
					,@DoanhSoBotBa
					,@DoanhSoSon
					,@IsActive
					,@UpdatedDate
					,@UpdatedBy
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the DoanhSoCamKetLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Update
(

	@Id int   ,

	@KhachHangDuThuongId uniqueidentifier   ,

	@Year int   ,

	@Quarter int   ,

	@Month int   ,

	@DoanhSoBase decimal (18, 0)  ,

	@DoanhSoBotBa decimal (18, 0)  ,

	@DoanhSoSon decimal (18, 0)  ,

	@IsActive bit   ,

	@UpdatedDate datetime   ,

	@UpdatedBy nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DoanhSoCamKetLapMayPhaMau]
				SET
					[KhachHangDuThuongID] = @KhachHangDuThuongId
					,[Year] = @Year
					,[Quarter] = @Quarter
					,[Month] = @Month
					,[DoanhSoBase] = @DoanhSoBase
					,[DoanhSoBotBa] = @DoanhSoBotBa
					,[DoanhSoSon] = @DoanhSoSon
					,[IsActive] = @IsActive
					,[UpdatedDate] = @UpdatedDate
					,[UpdatedBy] = @UpdatedBy
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the DoanhSoCamKetLapMayPhaMau table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[DoanhSoCamKetLapMayPhaMau] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the DoanhSoCamKetLapMayPhaMau table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[KhachHangDuThuongID],
					[Year],
					[Quarter],
					[Month],
					[DoanhSoBase],
					[DoanhSoBotBa],
					[DoanhSoSon],
					[IsActive],
					[UpdatedDate],
					[UpdatedBy]
				FROM
					[dbo].[DoanhSoCamKetLapMayPhaMau]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKetLapMayPhaMau_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKetLapMayPhaMau_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the DoanhSoCamKetLapMayPhaMau table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKetLapMayPhaMau_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@KhachHangDuThuongId uniqueidentifier   = null ,

	@Year int   = null ,

	@Quarter int   = null ,

	@Month int   = null ,

	@DoanhSoBase decimal (18, 0)  = null ,

	@DoanhSoBotBa decimal (18, 0)  = null ,

	@DoanhSoSon decimal (18, 0)  = null ,

	@IsActive bit   = null ,

	@UpdatedDate datetime   = null ,

	@UpdatedBy nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [KhachHangDuThuongID]
	, [Year]
	, [Quarter]
	, [Month]
	, [DoanhSoBase]
	, [DoanhSoBotBa]
	, [DoanhSoSon]
	, [IsActive]
	, [UpdatedDate]
	, [UpdatedBy]
    FROM
	[dbo].[DoanhSoCamKetLapMayPhaMau]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([KhachHangDuThuongID] = @KhachHangDuThuongId OR @KhachHangDuThuongId is null)
	AND ([Year] = @Year OR @Year is null)
	AND ([Quarter] = @Quarter OR @Quarter is null)
	AND ([Month] = @Month OR @Month is null)
	AND ([DoanhSoBase] = @DoanhSoBase OR @DoanhSoBase is null)
	AND ([DoanhSoBotBa] = @DoanhSoBotBa OR @DoanhSoBotBa is null)
	AND ([DoanhSoSon] = @DoanhSoSon OR @DoanhSoSon is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([UpdatedDate] = @UpdatedDate OR @UpdatedDate is null)
	AND ([UpdatedBy] = @UpdatedBy OR @UpdatedBy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [KhachHangDuThuongID]
	, [Year]
	, [Quarter]
	, [Month]
	, [DoanhSoBase]
	, [DoanhSoBotBa]
	, [DoanhSoSon]
	, [IsActive]
	, [UpdatedDate]
	, [UpdatedBy]
    FROM
	[dbo].[DoanhSoCamKetLapMayPhaMau]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([KhachHangDuThuongID] = @KhachHangDuThuongId AND @KhachHangDuThuongId is not null)
	OR ([Year] = @Year AND @Year is not null)
	OR ([Quarter] = @Quarter AND @Quarter is not null)
	OR ([Month] = @Month AND @Month is not null)
	OR ([DoanhSoBase] = @DoanhSoBase AND @DoanhSoBase is not null)
	OR ([DoanhSoBotBa] = @DoanhSoBotBa AND @DoanhSoBotBa is not null)
	OR ([DoanhSoSon] = @DoanhSoSon AND @DoanhSoSon is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([UpdatedDate] = @UpdatedDate AND @UpdatedDate is not null)
	OR ([UpdatedBy] = @UpdatedBy AND @UpdatedBy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the DieuKienGiamGia table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_Get_List

AS


				
				SELECT
					[DieuKienID],
					[DieuKienChung],
					[LastUpdate],
					[UpdateBy],
					[IsActive]
				FROM
					[dbo].[DieuKienGiamGia]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the DieuKienGiamGia table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [DieuKienID] uniqueidentifier 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([DieuKienID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [DieuKienID]'
				SET @SQL = @SQL + ' FROM [dbo].[DieuKienGiamGia]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[DieuKienID], O.[DieuKienChung], O.[LastUpdate], O.[UpdateBy], O.[IsActive]
				FROM
				    [dbo].[DieuKienGiamGia] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[DieuKienID] = PageIndex.[DieuKienID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DieuKienGiamGia]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the DieuKienGiamGia table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_Insert
(

	@DieuKienId uniqueidentifier   ,

	@DieuKienChung nvarchar (MAX)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@IsActive bit   
)
AS


					
				INSERT INTO [dbo].[DieuKienGiamGia]
					(
					[DieuKienID]
					,[DieuKienChung]
					,[LastUpdate]
					,[UpdateBy]
					,[IsActive]
					)
				VALUES
					(
					@DieuKienId
					,@DieuKienChung
					,@LastUpdate
					,@UpdateBy
					,@IsActive
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the DieuKienGiamGia table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_Update
(

	@DieuKienId uniqueidentifier   ,

	@OriginalDieuKienId uniqueidentifier   ,

	@DieuKienChung nvarchar (MAX)  ,

	@LastUpdate datetime   ,

	@UpdateBy int   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DieuKienGiamGia]
				SET
					[DieuKienID] = @DieuKienId
					,[DieuKienChung] = @DieuKienChung
					,[LastUpdate] = @LastUpdate
					,[UpdateBy] = @UpdateBy
					,[IsActive] = @IsActive
				WHERE
[DieuKienID] = @OriginalDieuKienId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the DieuKienGiamGia table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_Delete
(

	@DieuKienId uniqueidentifier   
)
AS


				DELETE FROM [dbo].[DieuKienGiamGia] WITH (ROWLOCK) 
				WHERE
					[DieuKienID] = @DieuKienId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_GetByDieuKienId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_GetByDieuKienId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_GetByDieuKienId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the DieuKienGiamGia table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_GetByDieuKienId
(

	@DieuKienId uniqueidentifier   
)
AS


				SELECT
					[DieuKienID],
					[DieuKienChung],
					[LastUpdate],
					[UpdateBy],
					[IsActive]
				FROM
					[dbo].[DieuKienGiamGia]
				WHERE
					[DieuKienID] = @DieuKienId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuKienGiamGia_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuKienGiamGia_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuKienGiamGia_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the DieuKienGiamGia table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuKienGiamGia_Find
(

	@SearchUsingOR bit   = null ,

	@DieuKienId uniqueidentifier   = null ,

	@DieuKienChung nvarchar (MAX)  = null ,

	@LastUpdate datetime   = null ,

	@UpdateBy int   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [DieuKienID]
	, [DieuKienChung]
	, [LastUpdate]
	, [UpdateBy]
	, [IsActive]
    FROM
	[dbo].[DieuKienGiamGia]
    WHERE 
	 ([DieuKienID] = @DieuKienId OR @DieuKienId is null)
	AND ([DieuKienChung] = @DieuKienChung OR @DieuKienChung is null)
	AND ([LastUpdate] = @LastUpdate OR @LastUpdate is null)
	AND ([UpdateBy] = @UpdateBy OR @UpdateBy is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [DieuKienID]
	, [DieuKienChung]
	, [LastUpdate]
	, [UpdateBy]
	, [IsActive]
    FROM
	[dbo].[DieuKienGiamGia]
    WHERE 
	 ([DieuKienID] = @DieuKienId AND @DieuKienId is not null)
	OR ([DieuKienChung] = @DieuKienChung AND @DieuKienChung is not null)
	OR ([LastUpdate] = @LastUpdate AND @LastUpdate is not null)
	OR ([UpdateBy] = @UpdateBy AND @UpdateBy is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the DoanhSoCamKet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_Get_List

AS


				
				SELECT
					[ID],
					[KhachHangDuThuongID],
					[Year],
					[Quarter],
					[Month],
					[Doanhso],
					[IsActive],
					[DoanhSoLapQuy]
				FROM
					[dbo].[DoanhSoCamKet]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the DoanhSoCamKet table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[DoanhSoCamKet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[KhachHangDuThuongID], O.[Year], O.[Quarter], O.[Month], O.[Doanhso], O.[IsActive], O.[DoanhSoLapQuy]
				FROM
				    [dbo].[DoanhSoCamKet] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DoanhSoCamKet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the DoanhSoCamKet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_Insert
(

	@Id int    OUTPUT,

	@KhachHangDuThuongId uniqueidentifier   ,

	@Year int   ,

	@Quarter int   ,

	@Month int   ,

	@Doanhso decimal (18, 0)  ,

	@IsActive bit   ,

	@DoanhSoLapQuy decimal (18, 0)  
)
AS


					
				INSERT INTO [dbo].[DoanhSoCamKet]
					(
					[KhachHangDuThuongID]
					,[Year]
					,[Quarter]
					,[Month]
					,[Doanhso]
					,[IsActive]
					,[DoanhSoLapQuy]
					)
				VALUES
					(
					@KhachHangDuThuongId
					,@Year
					,@Quarter
					,@Month
					,@Doanhso
					,@IsActive
					,@DoanhSoLapQuy
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the DoanhSoCamKet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_Update
(

	@Id int   ,

	@KhachHangDuThuongId uniqueidentifier   ,

	@Year int   ,

	@Quarter int   ,

	@Month int   ,

	@Doanhso decimal (18, 0)  ,

	@IsActive bit   ,

	@DoanhSoLapQuy decimal (18, 0)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DoanhSoCamKet]
				SET
					[KhachHangDuThuongID] = @KhachHangDuThuongId
					,[Year] = @Year
					,[Quarter] = @Quarter
					,[Month] = @Month
					,[Doanhso] = @Doanhso
					,[IsActive] = @IsActive
					,[DoanhSoLapQuy] = @DoanhSoLapQuy
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the DoanhSoCamKet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[DoanhSoCamKet] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the DoanhSoCamKet table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[KhachHangDuThuongID],
					[Year],
					[Quarter],
					[Month],
					[Doanhso],
					[IsActive],
					[DoanhSoLapQuy]
				FROM
					[dbo].[DoanhSoCamKet]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DoanhSoCamKet_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DoanhSoCamKet_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DoanhSoCamKet_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the DoanhSoCamKet table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DoanhSoCamKet_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@KhachHangDuThuongId uniqueidentifier   = null ,

	@Year int   = null ,

	@Quarter int   = null ,

	@Month int   = null ,

	@Doanhso decimal (18, 0)  = null ,

	@IsActive bit   = null ,

	@DoanhSoLapQuy decimal (18, 0)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [KhachHangDuThuongID]
	, [Year]
	, [Quarter]
	, [Month]
	, [Doanhso]
	, [IsActive]
	, [DoanhSoLapQuy]
    FROM
	[dbo].[DoanhSoCamKet]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([KhachHangDuThuongID] = @KhachHangDuThuongId OR @KhachHangDuThuongId is null)
	AND ([Year] = @Year OR @Year is null)
	AND ([Quarter] = @Quarter OR @Quarter is null)
	AND ([Month] = @Month OR @Month is null)
	AND ([Doanhso] = @Doanhso OR @Doanhso is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
	AND ([DoanhSoLapQuy] = @DoanhSoLapQuy OR @DoanhSoLapQuy is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [KhachHangDuThuongID]
	, [Year]
	, [Quarter]
	, [Month]
	, [Doanhso]
	, [IsActive]
	, [DoanhSoLapQuy]
    FROM
	[dbo].[DoanhSoCamKet]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([KhachHangDuThuongID] = @KhachHangDuThuongId AND @KhachHangDuThuongId is not null)
	OR ([Year] = @Year AND @Year is not null)
	OR ([Quarter] = @Quarter AND @Quarter is not null)
	OR ([Month] = @Month AND @Month is not null)
	OR ([Doanhso] = @Doanhso AND @Doanhso is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([DoanhSoLapQuy] = @DoanhSoLapQuy AND @DoanhSoLapQuy is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the ThuongHieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_Get_List

AS


				
				SELECT
					[BrandID],
					[BrandName],
					[BrandCode],
					[IsActive]
				FROM
					[dbo].[ThuongHieu]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the ThuongHieu table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [BrandID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([BrandID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [BrandID]'
				SET @SQL = @SQL + ' FROM [dbo].[ThuongHieu]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[BrandID], O.[BrandName], O.[BrandCode], O.[IsActive]
				FROM
				    [dbo].[ThuongHieu] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[BrandID] = PageIndex.[BrandID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ThuongHieu]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the ThuongHieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_Insert
(

	@BrandId int    OUTPUT,

	@BrandName nvarchar (50)  ,

	@BrandCode nvarchar (10)  ,

	@IsActive bit   
)
AS


					
				INSERT INTO [dbo].[ThuongHieu]
					(
					[BrandName]
					,[BrandCode]
					,[IsActive]
					)
				VALUES
					(
					@BrandName
					,@BrandCode
					,@IsActive
					)
				
				-- Get the identity value
				SET @BrandId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the ThuongHieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_Update
(

	@BrandId int   ,

	@BrandName nvarchar (50)  ,

	@BrandCode nvarchar (10)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ThuongHieu]
				SET
					[BrandName] = @BrandName
					,[BrandCode] = @BrandCode
					,[IsActive] = @IsActive
				WHERE
[BrandID] = @BrandId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the ThuongHieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_Delete
(

	@BrandId int   
)
AS


				DELETE FROM [dbo].[ThuongHieu] WITH (ROWLOCK) 
				WHERE
					[BrandID] = @BrandId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_GetByBrandId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_GetByBrandId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_GetByBrandId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the ThuongHieu table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_GetByBrandId
(

	@BrandId int   
)
AS


				SELECT
					[BrandID],
					[BrandName],
					[BrandCode],
					[IsActive]
				FROM
					[dbo].[ThuongHieu]
				WHERE
					[BrandID] = @BrandId
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThuongHieu_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThuongHieu_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThuongHieu_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the ThuongHieu table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThuongHieu_Find
(

	@SearchUsingOR bit   = null ,

	@BrandId int   = null ,

	@BrandName nvarchar (50)  = null ,

	@BrandCode nvarchar (10)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [BrandID]
	, [BrandName]
	, [BrandCode]
	, [IsActive]
    FROM
	[dbo].[ThuongHieu]
    WHERE 
	 ([BrandID] = @BrandId OR @BrandId is null)
	AND ([BrandName] = @BrandName OR @BrandName is null)
	AND ([BrandCode] = @BrandCode OR @BrandCode is null)
	AND ([IsActive] = @IsActive OR @IsActive is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [BrandID]
	, [BrandName]
	, [BrandCode]
	, [IsActive]
    FROM
	[dbo].[ThuongHieu]
    WHERE 
	 ([BrandID] = @BrandId AND @BrandId is not null)
	OR ([BrandName] = @BrandName AND @BrandName is not null)
	OR ([BrandCode] = @BrandCode AND @BrandCode is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the Doanh_So table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_Get_List

AS


				
				SELECT
					[ID],
					[So_Hoa_Don],
					[Ngay_Lay_Hang],
					[Khach_Hang_ID],
					[InventoryItemID],
					[So_Luong],
					[Don_Gia],
					[Thanh_Tien],
					[Ghi_Chu]
				FROM
					[dbo].[Doanh_So]
					
				Select @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the Doanh_So table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				Create Table #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				declare @SQL as nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Doanh_So]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				exec sp_executesql @SQL

				-- Return paged results
				SELECT O.[ID], O.[So_Hoa_Don], O.[Ngay_Lay_Hang], O.[Khach_Hang_ID], O.[InventoryItemID], O.[So_Luong], O.[Don_Gia], O.[Thanh_Tien], O.[Ghi_Chu]
				FROM
				    [dbo].[Doanh_So] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) as TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Doanh_So]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				exec sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Inserts a record into the Doanh_So table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_Insert
(

	@Id int    OUTPUT,

	@SoHoaDon varchar (50)  ,

	@NgayLayHang datetime   ,

	@KhachHangId uniqueidentifier   ,

	@InventoryItemId uniqueidentifier   ,

	@SoLuong int   ,

	@DonGia decimal (18, 0)  ,

	@ThanhTien decimal (18, 0)  ,

	@GhiChu text   
)
AS


					
				INSERT INTO [dbo].[Doanh_So]
					(
					[So_Hoa_Don]
					,[Ngay_Lay_Hang]
					,[Khach_Hang_ID]
					,[InventoryItemID]
					,[So_Luong]
					,[Don_Gia]
					,[Thanh_Tien]
					,[Ghi_Chu]
					)
				VALUES
					(
					@SoHoaDon
					,@NgayLayHang
					,@KhachHangId
					,@InventoryItemId
					,@SoLuong
					,@DonGia
					,@ThanhTien
					,@GhiChu
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Updates a record in the Doanh_So table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_Update
(

	@Id int   ,

	@SoHoaDon varchar (50)  ,

	@NgayLayHang datetime   ,

	@KhachHangId uniqueidentifier   ,

	@InventoryItemId uniqueidentifier   ,

	@SoLuong int   ,

	@DonGia decimal (18, 0)  ,

	@ThanhTien decimal (18, 0)  ,

	@GhiChu text   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Doanh_So]
				SET
					[So_Hoa_Don] = @SoHoaDon
					,[Ngay_Lay_Hang] = @NgayLayHang
					,[Khach_Hang_ID] = @KhachHangId
					,[InventoryItemID] = @InventoryItemId
					,[So_Luong] = @SoLuong
					,[Don_Gia] = @DonGia
					,[Thanh_Tien] = @ThanhTien
					,[Ghi_Chu] = @GhiChu
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Deletes a record in the Doanh_So table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Doanh_So] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_GetByKhachHangId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_GetByKhachHangId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_GetByKhachHangId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the Doanh_So table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_GetByKhachHangId
(

	@KhachHangId uniqueidentifier   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ID],
					[So_Hoa_Don],
					[Ngay_Lay_Hang],
					[Khach_Hang_ID],
					[InventoryItemID],
					[So_Luong],
					[Don_Gia],
					[Thanh_Tien],
					[Ghi_Chu]
				FROM
					[dbo].[Doanh_So]
				WHERE
					[Khach_Hang_ID] = @KhachHangId
				
				Select @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Select records from the Doanh_So table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[So_Hoa_Don],
					[Ngay_Lay_Hang],
					[Khach_Hang_ID],
					[InventoryItemID],
					[So_Luong],
					[Don_Gia],
					[Thanh_Tien],
					[Ghi_Chu]
				FROM
					[dbo].[Doanh_So]
				WHERE
					[ID] = @Id
			Select @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Doanh_So_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Doanh_So_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Doanh_So_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Finds records in the Doanh_So table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Doanh_So_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@SoHoaDon varchar (50)  = null ,

	@NgayLayHang datetime   = null ,

	@KhachHangId uniqueidentifier   = null ,

	@InventoryItemId uniqueidentifier   = null ,

	@SoLuong int   = null ,

	@DonGia decimal (18, 0)  = null ,

	@ThanhTien decimal (18, 0)  = null ,

	@GhiChu text   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [So_Hoa_Don]
	, [Ngay_Lay_Hang]
	, [Khach_Hang_ID]
	, [InventoryItemID]
	, [So_Luong]
	, [Don_Gia]
	, [Thanh_Tien]
	, [Ghi_Chu]
    FROM
	[dbo].[Doanh_So]
    WHERE 
	 ([ID] = @Id OR @Id is null)
	AND ([So_Hoa_Don] = @SoHoaDon OR @SoHoaDon is null)
	AND ([Ngay_Lay_Hang] = @NgayLayHang OR @NgayLayHang is null)
	AND ([Khach_Hang_ID] = @KhachHangId OR @KhachHangId is null)
	AND ([InventoryItemID] = @InventoryItemId OR @InventoryItemId is null)
	AND ([So_Luong] = @SoLuong OR @SoLuong is null)
	AND ([Don_Gia] = @DonGia OR @DonGia is null)
	AND ([Thanh_Tien] = @ThanhTien OR @ThanhTien is null)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [So_Hoa_Don]
	, [Ngay_Lay_Hang]
	, [Khach_Hang_ID]
	, [InventoryItemID]
	, [So_Luong]
	, [Don_Gia]
	, [Thanh_Tien]
	, [Ghi_Chu]
    FROM
	[dbo].[Doanh_So]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([So_Hoa_Don] = @SoHoaDon AND @SoHoaDon is not null)
	OR ([Ngay_Lay_Hang] = @NgayLayHang AND @NgayLayHang is not null)
	OR ([Khach_Hang_ID] = @KhachHangId AND @KhachHangId is not null)
	OR ([InventoryItemID] = @InventoryItemId AND @InventoryItemId is not null)
	OR ([So_Luong] = @SoLuong AND @SoLuong is not null)
	OR ([Don_Gia] = @DonGia AND @DonGia is not null)
	OR ([Thanh_Tien] = @ThanhTien AND @ThanhTien is not null)
	Select @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwAllCustomers_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwAllCustomers_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwAllCustomers_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwAllCustomers view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwAllCustomers_Get_List

AS


				
				SELECT
					[KhachHangID],
					[MaKH],
					[TenKH],
					[LEVEL],
					[ParentID],
					[BranchID],
					[BranchCode],
					[Tel],
					[Address]
				FROM
					[dbo].[vwAllCustomers]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwAllCustomers_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwAllCustomers_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwAllCustomers_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwAllCustomers view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwAllCustomers_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwAllCustomers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwDoanhSo_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwDoanhSo_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwDoanhSo_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwDoanhSo view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwDoanhSo_Get_List

AS


				
				SELECT
					[ID],
					[So_Hoa_Don],
					[Ngay_Lay_Hang],
					[Khach_Hang_ID],
					[Thanh_Tien],
					[InventoryItemID],
					[So_Luong],
					[Don_Gia],
					[Ghi_Chu]
				FROM
					[dbo].[vwDoanhSo]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwDoanhSo_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwDoanhSo_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwDoanhSo_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwDoanhSo view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwDoanhSo_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwDoanhSo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwGetAllDoanhSo_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwGetAllDoanhSo_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwGetAllDoanhSo_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwGetAllDoanhSo view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwGetAllDoanhSo_Get_List

AS


				
				SELECT
					[AccountingObjectID],
					[MaHang],
					[PhanLoaiHang],
					[TenHang],
					[DonVi],
					[DoanhSoTrongThang],
					[SoLuong],
					[SoHoaDon],
					[NgayHoaDon]
				FROM
					[dbo].[vwGetAllDoanhSo]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwGetAllDoanhSo_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwGetAllDoanhSo_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwGetAllDoanhSo_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwGetAllDoanhSo view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwGetAllDoanhSo_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwGetAllDoanhSo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwGetTotalMonthlyRevenue_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwGetTotalMonthlyRevenue_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwGetTotalMonthlyRevenue_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwGetTotalMonthlyRevenue view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwGetTotalMonthlyRevenue_Get_List

AS


				
				SELECT
					[KhachHangID],
					[Thang],
					[Nam],
					[ThuongHieu],
					[DoanhSoTD],
					[DoanhSoCC]
				FROM
					[dbo].[vwGetTotalMonthlyRevenue]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwGetTotalMonthlyRevenue_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwGetTotalMonthlyRevenue_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwGetTotalMonthlyRevenue_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwGetTotalMonthlyRevenue view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwGetTotalMonthlyRevenue_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwGetTotalMonthlyRevenue]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwInventoryItem_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwInventoryItem_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwInventoryItem_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwInventoryItem view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwInventoryItem_Get_List

AS


				
				SELECT
					[InventoryItemID],
					[InventoryItemName],
					[InventoryItemCode],
					[Unit],
					[SalePrice]
				FROM
					[dbo].[vwInventoryItem]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwInventoryItem_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwInventoryItem_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwInventoryItem_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwInventoryItem view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwInventoryItem_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwInventoryItem]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwKhachHangLapMayPhaMau_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwKhachHangLapMayPhaMau_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwKhachHangLapMayPhaMau_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwKhachHangLapMayPhaMau view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwKhachHangLapMayPhaMau_Get_List

AS


				
				SELECT
					[AccountingObjectID],
					[AccountingObjectCode],
					[AccountingObjectName],
					[ID],
					[KhachHangID],
					[LoaiMay],
					[DoanhSoToiThieu],
					[IsActive],
					[ThoiDiemLapMay]
				FROM
					[dbo].[vwKhachHangLapMayPhaMau]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwKhachHangLapMayPhaMau_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwKhachHangLapMayPhaMau_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwKhachHangLapMayPhaMau_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwKhachHangLapMayPhaMau view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwKhachHangLapMayPhaMau_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwKhachHangLapMayPhaMau]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwMisaBranches_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwMisaBranches_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwMisaBranches_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwMisaBranches view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwMisaBranches_Get_List

AS


				
				SELECT
					[BranchID],
					[BranchCode],
					[BranchName],
					[IsSystem]
				FROM
					[dbo].[vwMisaBranches]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwMisaBranches_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwMisaBranches_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwMisaBranches_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwMisaBranches view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwMisaBranches_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwMisaBranches]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwMisaCustomers_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwMisaCustomers_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwMisaCustomers_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets all records from the vwMisaCustomers view
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwMisaCustomers_Get_List

AS


				
				SELECT
					[AccountingObjectID],
					[AccountingObjectCode],
					[AccountingObjectName]
				FROM
					[dbo].[vwMisaCustomers]
					
				Select @@ROWCOUNT			
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.vwMisaCustomers_Get procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.vwMisaCustomers_Get') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.vwMisaCustomers_Get
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA (www.atagroup.vn)
-- Purpose: Gets records from the vwMisaCustomers view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.vwMisaCustomers_Get
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  
)
AS


				
				BEGIN

				-- Build the sql query
				declare @SQL as nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vwMisaCustomers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				exec sp_executesql @SQL
				
				-- Return total count
				Select @@ROWCOUNT as TotalRowCount
				
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

